I would like to ask a question about the wisdom of having two cookies. In one PHP file, the following codes are to be found:
//dbconn establishes connection to database and is not shown here
$salt = hash("sha512", rand().rand().rand());
mysqli_query($dbconn, "INSERT into `members` (`membername`, `password`,
`salt`) VALUES ('$membername', '$password', '$salt')");
setcookie("cookiemember", hash("sha512", $membername,
time() + 24*60*60, "/");
setcookie("cookiesalt", $salt, time() + 24*60*60, "/");
mysqli_close($dbconn)
In a second PHP file, the following lines of code are found:
if (isset($_COOKIE['cookiemember']) &&
isset($_COOKIE['cookiesalt'])) {
$cookymem = mysqli_real_escape_string($dbconn,
$_COOKIE['cookiemember']);
$cookysalty = mysqli_real_escape_string($dbconn,
$_COOKIE['cookiesalt']);
$result = mysqli_query($dbconn, "SELECT * FROM `members`
where `salt` = '$cookysalty'");
What I don't understand is the purpose of the variable $salt. You take three randomly generated numbers, concatenate them, apply a secure hash algorithm to this concatenated number, and then put the output into a table called members
. You then use this $salt variable as the value of a cookie but not before you escape special characters from it. Why go through all this trouble when you are already encrypting the field name membername
by hashing it? Why can't the last line of the code simply read:
$result = mysqli_query($dbconn, "SELECT * FROM `members`
where `membername` = '$cookymem'");