Notes:
- I'm still fairly new to php
- This login form seems to work perfectly fine if I don't hash the password.
- I've tried md5, sha256 and now I've left it at sha1. None of these work.
- I first used
echo sha1("password");to find out what the hash for my password would be, I then copied that hash and pasted it manually with phpMyAdmin. I'm not sure if this is what the issue is or not.
Anyways here is the code:
<?php
session_start();
require("config.php");
if(isset($_POST['submit'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = sha1(mysql_real_escape_string($_POST['password']));
$loginsql = "SELECT * FROM login WHERE username = '" . $username .
"' AND password = '" . $password . "'";
$loginresult = mysql_query($loginsql);
$loginnumrows = mysql_num_rows($loginresult);
if($loginnumrows == 1) {
$loginrow = mysql_fetch_assoc($loginresult);
session_register("USERNAME");
session_register("USERID");
$_SESSION['USERNAME'] = $loginrow['username'];
$_SESSION['USERID'] = $loginrow['id'];
header("Location: " . $config_basedir . "controlpanel.php");
}
else{
echo "<p>Incorrect Login, please try again!</p>";
}
}
else{
}
?>
I'm really not too sure where to go with this. I'm sure my code could be more efficient but as I mentioned in the notes, it does work when I don't hash the password. Thank you for reading.
passwordsetup in your database?mysql_real_escape_stringuntil right before the data is sent to the database. Specifically,sha1(mysql_real_escape_string($_POST['password']));should bemysql_real_escape_string(sha1($_POST['password']));. We also can't see how the stored password is hashed, so we can't really help you. Try selecting the record andvar_dumping your password. Compare this with the value being posted to the server. These are basic debugging steps you should learn to follow.$password = sha1($_POST['password']);? No need for mysql_real_escape_string here.mysql_real_escape_string? If not, and your password contains special characters it could get mangled bymysql_real_escape_string. You need to do it the same way both times.