Jump to content

Retrieve from MySQL a previously encripted string


ruano84

Recommended Posts

Hi,

I am writing an user/password validator in PHP and MySQL. I am using the function crypt() to encrypt the passwords, store them in a table, and then when the users introduce their password, retrieve the stored one and compare them. This is the code to crypt and insert the password:

$name="a name";
$password=crypt("a password");
$dbh=mysql_connect ("localhost", "user");
mysql_select_db ("DataBase");
mysql_query("INSERT INTO atable (name,password) values ('$name','$password')");

And this one is for retrieve it:

$dbh=mysql_connect ("localhost", "user");
mysql_select_db ("DataBase");
$res=mysql_query("SELECT * FROM usuarios WHERE name='a name' ");
$row=mysql_fetch_row($res);
$password=crypt("a password",$row[1]);
if($password==$row[1]) echo "yes";
if($password!=$row[1]) echo "no";

For some reason, the hash of the 2nd call to the crypt function is not returning the original, so echoes "no". What could be wrong?

Thanks,
Alexis RR
Heres a quick md5 example ok

save  as a.php
[code]
<?php

$password="redarrow";

if(isset($_POST['submit'])){

if($pass==$password){

echo "Hello $name <br> This is your password in md5 format
".md5($password)." ";

}else{

echo "Sorry $name wrong password <a href='a.php'>Try Agin</a>";
exit;
}
}
?>


<form method="POST" action="a.php">
<br>
Your name please
<br>
<input type="text" name="name">
<br>
Your password please
<br>
<input type="password" name="pass">
<br>
<br>
<input type="submit" name="submit" value="send">

</form>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.