Here is my code for the login script. Everything works perfectly, but everytime I enter everything CORRECTLY into the forum, it says "The username, ____, and password do not match!". When they do match.
If I leave the areas blank, they say "You must enter a username!" or "You must enter a password!".
All the error messages work good, but whenever I fill the form in correctly, it displays my first error message "The username, ____, and password do not match!".
Does anyone see what's wrong with it?

<?php
session_start();
include("config.php");

$username = $_POST['username'];
$usernamefinal = ucfirst(strtolower($username));
$password = $_POST['password'];

if (isset($_POST['submit']))
{
	if(!empty($username))
	{
		if (!empty($password))
		{
			$sql = "SELECT username FROM members WHERE username='$usernamefinal'";
			$result = mysqli_query($cxn, $sql) or die("Query died: username");
			$num = mysqli_num_rows($result);
			if ($num > 0)
			{
				$sql = "SELECT username, password FROM members WHERE username='$usernamefinal' AND password=md5('$password')";
				$result = mysqli_query($cxn, $sql) or die("Query died: username and password");
				$num = mysqli_num_rows($result);
				if ($num > 0)
				{
					$sql = "SELECT userid FROM members WHERE username='$usernamefinal'";
					$result = mysqli_query($cxn, $sql) or die("Query died: userid");
					$row = mysqli_fetch_array($result);
					$userid = $row['userid'];
					
					$_SESSION['auth'] = "yes";
					$_SESSION['username'] = $usernamefinal;
					$_SESSION['userid'] = $userid;
					$ipadd = $_SERVER['REMOTE_ADDR'];
					$sql2 = "INSERT INTO login (userid, username, logintime, ipadd) VALUES ('$userid', '$usernamefinal', NOW(), inet_aton('$ipadd'))";
					mysqli_query($cxn, $sql2) or die("Query died: login session");
					header("Location: news.php");
				}
				else
				{
					$error = "The username, $usernamefinal, and password do not match!";
				}
			}
			else
			{
				$error = "That username doesn't exist!";
			}
		}
		else
		{
			$error = "You must enter a password!";
		}
	}
	else
	{
		$error = "You must enter a username!";
	}
}
?>

<?php include("header.php"); ?>
	
	<h1>Login Form</h1>
	<?php echo $error; ?>
	<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
		Username: <input type="text" name="username"><br>
		Password: <input type="password" name="password"><br>
		<input type="submit" name="submit" value="Login">
	</form>
				
<?php include("footer.php"); ?>

Dani AI

Generated

A likely cause is a mismatch between how the password was hashed when the account was created and how it is checked at login. is right that an algorithm mix-up can happen, but the two most common practical reasons are (a) the database column truncates the stored hash, or (b) the stored form is raw binary while the login code is comparing hex text (or vice versa).

Useful checks to run (no changes required yet):

DESCRIBE members;
SELECT LENGTH(password), HEX(password) FROM members WHERE username = 'someuser';

Also inspect the value returned in PHP with strlen($stored) and bin2hex($stored) to see whether the stored value is 16 raw bytes or a 32-character hex string. If LENGTH shows 16 but the login compares against a 32-byte hex string (or the column is shorter than 32), the comparison will always fail.

How to fix, depending on the result:

  • If the stored values are hex strings, make the column CHAR(32) or VARCHAR(32) and ensure both registration and login use the same hex output (PHP md5() default or MySQL MD5()).
  • If the stored values are raw bytes, use BINARY(16) and convert hex to raw with UNHEX() (or PHP hex2bin()) when inserting or comparing.
  • If the column is too short, alter it and convert existing rows consistently (e.g., UPDATE ... SET password = UNHEX(password) before changing to BINARY(16), or convert to hex and use CHAR(32)).

Notes and cautions: md5() has a second parameter that produces raw binary output; mixing raw and hex is a frequent source of problems. Also MD5 is not appropriate for storing passwords today — migrate to password_hash()/password_verify() and use prepared statements to avoid SQL injection.

References: PHP md5() docs (https://www.php.net/manual/en/function.md5.php), MySQL MD5() docs (https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html#function_md5), MySQL binary types (https://dev.mysql.com/doc/refman/8.0/en/binary-varbinary.html), PHP password hashing (https://www.php.net/manual/en/function.password-hash.php).

Ok I have the variables output after the form is submitted.
The password is what's wrong.

It's suppose to be this:
7da293f88d6e3bffc85a5e86e

And it's coming out like this:
7da293f88d6e3bffc85a5e86ee836fca

Do you have any clue why it is doing that? It's adding 7 extra characters onto it.

Member Avatar for Member #900861

Seems like you may have got your encryption method mixed up somewhere. Maybe you used MD2 or MD4 instead of MD5 somewhere or the other way round

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.