GOAL: Trying to create a login. The registration page uses this to create the username and password based on input:
Register
<?php
$host="localhost"; // Host name
$username="root"; 开发者_运维问答// Mysql username
$password=""; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="Student"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
/* Obliterate bad input */
$secUser = mysql_real_escape_string($_POST['reguser']);
$badpasses = $_POST['regpass'];
$salt = '~Z`!@#$%I^&*()_-+Q=}]{[\|"><';
$secPass = hash('sha512', $badpasses.$salt);
$sql= "INSERT INTO Student (uname, upass, fname, lname, email, currGrade) VALUES('$secUser','$secPass','$_POST[regfirst]','$_POST[reglast]','$_POST[regemail]','$_POST[regclassrank]')";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else{
echo "Registered";
}
?>
Login.php
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="Student"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$salt = '~Z`!@#$%I^&*()_-+Q=}]{[\|"><';
$myusername = $_POST['uname'];
$mypassword = $_POST['upass'];
$mypassword = hash('sha512', $mypassword.$salt);
$sql = "SELECT * FROM $tbl_name WHERE uname = '$myusername' AND upass = '$mypassword')";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("regduser");
session_register("regdpass");
header("location:login_success.php");
}
else {
echo "$mypassword<br />";
echo "Wrong Username or Password";
}
ob_end_flush();
?>
The error I get is the Wrong User or Pass
, but the password when I try to login is:
bffbc4f94f40d0cece6774ed9ec792b03ad5362edf768d190913d033c46ad4af4e2cbe1d42134f58da402efb7d3209b7e9b62ff3e81caf6341262b24dd300e9a
the password in the database for the user poop
and password poop
in the db is:
4e4d252a08ac4c35c2917b4fc715fef13bac2b686c7ebc8f8256765bd584a89634df3fa455ed73c1fbec84d442f11d5e064749396dcb1c0f1525f82c1b0ea57a
Why are these passwords different?! Can someone help?
Should
// Define $myusername and $mypassword
$salt = '~Z`!@#$%I^&*()_-+Q=}]{[\|"><';
$myusername = $_POST['uname'];
$mypassword = $_POST['upass'];
$mypassword = hash('sha512', $mypassword);
not be
// Define $myusername and $mypassword
$salt = '~Z`!@#$%I^&*()_-+Q=}]{[\|"><';
$myusername = $_POST['uname'];
$mypassword = $_POST['upass'];
$mypassword = hash('sha512', $mypassword.$salt);
?
You add the salt to the password hash when you register, but you don't add it when you attempt a login.
I also want to strongly urge you not to POST directly into your database queries, but to use the mysql_real_escape for every POST variable that is used in a query. Better still would be to use prepared statements.
Doest that help at all?
I do not see where the "salt" is used in your login script. Maybe try the following:
$mypassword = hash('sha512', $mypassword . $salt);
Your $_POST['upass'];
variable is empty. Check if your form field is actually called upass
.
Based on this piece of information:
>> $salt = '~Z`!@#$%I^&*()_-+Q=}]{[\|"><';
'~Z`!@#$%I^&*()_-+Q=}]{[\\|"><'
>> hash('sha512', 'poop'.$salt);
'4e4d252a08ac4c35c2917b4fc715fef13bac2b686c7ebc8f8256765bd584a89634df3fa455ed73c1fbec84d442f11d5e064749396dcb1c0f1525f82c1b0ea57a'
>> hash('sha512', ''.$salt);
'bffbc4f94f40d0cece6774ed9ec792b03ad5362edf768d190913d033c46ad4af4e2cbe1d42134f58da402efb7d3209b7e9b62ff3e81caf6341262b24dd300e9a'
精彩评论