1
Good afternoon to all,
I have a problem that has been puzzling me for a few days now, my boss and trainee advisor proposed me to make an added security on the site and for that I would have to use a password hash, after a lot of research I could not log in the accounts previously "encrypted" and I can’t find a way to find my problem .
Registration page
include("connect.php");
function generateSalt(){
$characters = '0123456789abcdef';
$length = 64;
$string = '';
for ($max = mb_strlen($characters) - 1, $i = 0; $i < $length; ++ $i)
{
$string .= mb_substr($characters, mt_rand(0, $max), 1);
}
return $string;
}
$userAccountInput = $_POST['userName'];
$userPasswordInput = $_POST['pass'];
$salt = generateSalt();
$hash = hash_hmac("sha256", $userPasswordInput, $salt);
$sql = "INSERT INTO username VALUES (NULL,'$userAccountInput','$salt','$hash');";
$resultado = mysql_query ($sql);
Login page
function testPassword($fPassword, $fSaltFromDatabase, $fHashFromDatabase){
if (hash_hmac("sha256", $fPassword, $fSaltFromDatabase) === $fHashFromDatabase){
return(true);
}else{
return(false);
}
}
function SignIn()
{
//session_start();
if(!empty($_POST['userName']) || !empty($_POST['pass']))
{
$query = mysql_query("SELECT * FROM username where userName = '".$_POST['userName']."'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
//$userAccountInput = $_POST['userName'];
$userPasswordInput = $_POST['pass'];
$saltFromDatabase = $row['salt'];
$hashFromDatabase = $row['hash'];
$var_dump($row);
if(testPassword($userPasswordInput, $saltFromDatabase, $hashFromDatabase)){
echo "<script type='text/javascript'>alert('LOGIN COM SUCESSO!')</script>";
header('Location: ./clientes.php');
}else{
echo "<script type='text/javascript'>alert('LOGIN FALHADO!')</script>";
}
}
}
if(isset($_POST['submit']))
{
SignIn();
}
?>
I’m getting extremely frustrated because I’m already about 5 days away from changing the code and I can’t find a solution to this problem, I hope you can help, any help is welcome and very grateful, thank you all.
The function
mysql_query
should receive only oneSQL query
like a string. You’re sendingor die(mysql_error())
as a function parameter as well, and it probably shouldn’t be like this.– mutlei
Yes you’re right, I’ve formatted the code for that, but still the error remains.
– Vitor Trindade
The
$_POST[username]
asstring
is normal in PHP? It should not be separated fromstring
with a+
? Something like"SELECT * FROM username WHERE userName = " + $_POST[username]
– mutlei
I at least in the rest of my code never entered the "+" and always worked, I was missing the '', now I no longer see error to login, but does not return the echo of "Success" or "Failed", the page simply goes blank
– Vitor Trindade
Where is the implementation code of query insertion?
– Jorge B.
Thank you already managed to register in the database using this execution code
$resultado = mysql_query ($sql);
, but the login still doesn’t work– Vitor Trindade
You shouldn’t take
$saltFromDatabase = $row['salt'];
? I don’t understand why you’re taking$_REQUEST
.– Luis Henrique
I used the
$_REQUEST
to fetch the database the value, I modified it to your option but the result remains the same.– Vitor Trindade
Have you checked that the data has been entered in the database correctly? Its functions are working all right: https://ideone.com/ALapd7
– Luis Henrique
substitute
$_POST[userName]
for$_POST['userName']
... Missing are theaspas
– RodrigoBorth
Yes the registration already works correctly, when I create an account in the database
$userName
$salt
and the$hash
, but when I log in, the page is blank and does not return any of the echo’s I made– Vitor Trindade
I replaced how you tucked me in, but you gave me back this mistake Parse error: syntax error, Unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C: xampp htdocs insysupgrades login.php on line 20
– Vitor Trindade
$_POST[\"username\"]
, must escape the quotation marks.– Luis Henrique
even that way, returns me the same error that I abhor
– Vitor Trindade
Can make a
var_dump($row)
and edit including in your question? PS: change the query to:"SELECT * FROM username where userName = '".$_POST['userName']."'"
– Luis Henrique
I already changed the query to what you told me, and entered the
var_dump($row);
but I get nothing back ;– Vitor Trindade