Jump to content

Archived

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

ruano84

Retrieve from MySQL a previously encripted string

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

Share this post


Link to post
Share on other sites
Hmm, no I am wrong.  Ignore that :)  I must think before typing..

Share this post


Link to post
Share on other sites
Are you sure that $row[1] is actually the password?  Try printing out the values of $row[1], and also print out the result of that crypt() call.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Hi, Thanks for answering,

The md5() function does the same thing. I think it's a problem with MySQL, but i really don't know what's the problem.

Alexis RR

Share this post


Link to post
Share on other sites
Hi,

I saw all the code and realize that there was a programming error from me. I was hashing the password 2 times inside a validation process.

Thanks for everything

Share this post


Link to post
Share on other sites

×

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.