Can anyone help me using the crypt() function:
The password is encrypted in the database but when i go to login it says that the password is invalid
Here is my login code:
#!/usr/local/bin/php
<?php
// display individual record
if ((!$username) || (!$password)) {
echo "All fields were not completed";
exit;
}
elseif (($username)&&($password)) {
$db = mysql_connect("localhost", "ohbs", "X4aGnwSw");
mysql_select_db("ohbs",$db);
$result=mysql_query("SELECT * FROM customers WHERE username='$username'",$db);
$my_row = mysql_fetch_array($result);
$email = $my_row["email"];
$salt=(strlen($email));
$encpass=crypt($password, $salt);
$result1 = mysql_query("SELECT * FROM customers WHERE username='$username' AND password='$encpass'",$db);
$num=mysql_numrows($result1);
$myrow = mysql_fetch_array($result1);
if ($num!=0)
{
header("Location: http://www.redbrick.dcu.ie/~ohbs/front.html");
exit;
}
elseif ($num==0)
{
echo "Sorry, no records were found! Please go back to previous page and click on register.\n<br>";
echo"<a href='test7.html'>Back to logon page</a>";
}
}
?>
Here is my registering code:
#!/usr/local/bin/php
<?php
if ((!$username) || (!$password) || (!$email) || (!$first) || (!$last)||
(!$age)||(!$address) || (!$phone)) {
echo "All fields were not completed";
exit;
}
if((!ereg("[a-z]", $first)) || (!ereg("[a-z]", $last)))
{
echo "First and Last name not entered correctly";
exit;
}
if (!ereg("^[[:alpha:]]{4,8}$", $username) || !ereg("^[[:alpha:]]{4,10}$", $password))
{
echo " Username has to be between 4 and 8 chars and password has to be between 4 and 10";
exit;
}
if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
'@'.
'[-!#$%&\'*+\/0-9=?A-Z_`a-z{|}~]+.'.
'[-!#$%&\'*+\./0-9=?A-Z_`a-z{|}~]+$', $email))
{
echo "Invalid email address";
exit;
}
else
{
// process form
$db = mysql_connect("localhost", "ohbs", "X4aGnwSw");
mysql_select_db("ohbs",$db);
$salt=(strlen($email));
$encpass=crypt($password, $salt);
$sql = "INSERT INTO customers
(username,password,email,first,last,age,address,phone) VALUES
('$username','$encpass','$email','$first','$last','$age','$address','$phone')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n<br>";
echo"<a href='front.html'>Proceed to DCU Online Holiday Booking System.</a>";
}
?>