Skip to content Skip to sidebar Skip to footer

Php Log In Error Undefined Index

I am trying to log in using this code : session_start(); require 'connect.php'; $username = $_POST['username']; $password = $_POST['password']; if($username&&$passwor

Solution 1:

On a side note: you have an SQL injection there. Might want to read more: http://en.wikipedia.org/wiki/SQL_injection

The problem you are facing is that the username is not always POST'd (when you just load the page first time):

$username = isset($_POST['username']) ? $_POST['username'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;

That should fix it. Basically, I check if the POST index is set, and only if it is I try to access it, otherwise I set it to null.

Also, you might want to do it like this:

$query = mysql_query("SELECT * FROM users WHERE username='" . mysql_real_escape_string($username) . "'");

That prevents the SQL injection vulnerability.

And also add exit;:

header("Location: members.php");
$_SESSION['username']=$db_username;
exit; // Add this.

Solution 2:

Same as always. You're not POSTing to the URL. Verify the URL you're attempting to POST to.

Solution 3:

As it says, you don't have the specified data from POST. Make sure your form action is right and you're filling out the username.

Also, you might want to consider hashing your passwords. From what I can see here you compare plain text passwords (or you're already getting hashed passwords to your script, which would be ok).

Post a Comment for "Php Log In Error Undefined Index"