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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.