I've had this problem recently working on a project and the only thing that comes to my mind is:
"**Warning:** Cannot modify header information - headers already sent (output started at /htdocs/your_project_name/your_file.php:X, where X is the line number)."
Double check your errors; insert this somewhere on top in the script you are using to process the form submission:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
I was able to get it to work by doing this:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if ((isset($_POST['username']) && !empty($_POST['username'])) &&
(isset($_POST['password']) && !empty($_POST['password']))) {
//echo $_POST['username'];
//echo $_POST['password'];
// these statements are commented out because they were
// displaying information before the header('Location: index.php')
// was called, uncomment this and try to see what I am talking about
$username = $_POST['username'];
$password = $_POST['password'];
$db_username = 'root';
$db_password = 'root';
// always use try-catch when working with databases, api's, etc.
try
{
$dbconn = new PDO('mysql:host=localhost;dbname=db', $db_username, $db_password);
$stmt = $dbconn->prepare('SELECT * FROM users WHERE username=:username AND
password=:password LIMIT 1');
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();
if ($stmt->rowCount() > 0)
{
header('Location: index.php');
}
}
catch (PDOException $e)
{
echo 'Database error: ' . $e->getMessage();
}
}
Obviously, the form doesn't change, except that I added the action attribute:
<form method="post" action="submit.php">
<!--form fields-->
</form>
Obviously, make sure you are storing users passwords securely. I didn't hash my password before I checked the database but you should always sanitize any input that gets submitted from a form. Take a look here
- phpsec.org/projects/guide/1.html#1.4
and here
- php.net/manual/en/function.password-hash.php
And this in particular, as you would then use this function to check whether the password matches:
http://php.net/manual/en/function.password-verify.php
If you are still having problems and the headers already sent issue is the problem, check out this post:
How to fix "Headers already sent" error in PHP