2

I'm trying to make a simple register and login form. I want to use SHA1 to save the encrypted password in database. But when I try to login with the password, it seems it does not work. There are three files - index.php, register.php ,login.php

Please help me to solve this problem.

//index.php

<form action="register.php" method="post" enctype="multipart/form-data">

    <label for="email">Email:</label>
    <input type="text" name="email">
    <br />
    <label for="password">Password:</label>
    <input type="password" name="password">
    <button>Register</button>
</form>

<form action="login.php" method="post">
    <label for="email">Email:</label>
    <input type="text" name="email">
    <br />
    <label for="password">Password:</label>
    <input type="password" name="password">
    <button>Login</button>      
</form>

//register.php

<?php

$email = $_POST['email'];
$password = $_POST['password'];
$regist_day=date('d-m-Y (H:i)');

if (!empty($email) && !empty($password)) {

require_once('lib/db_connect.php');

$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)
        or die('Error connecting database');

$sql = "INSERT INTO member(email,password,regist_day)";
$sql .= "VALUES ('$email',SHA1('$password'),'$regist_day')";
mysqli_query($dbc,$sql);

echo("
    <script>
        location.href='try.php'
    </script>

    ") ;
}

else{
echo "You need to enter Email and Password";
}
?>

//login.php

<?php
$user_email = $_POST['email'];
$user_password = SHA1($_POST['password']);

if (!empty($user_email) && !empty($user_password)) {

require_once('lib/db_connect.php');

$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)
or die('Error connecting database');

$sql = "SELECT * FROM member WHERE email = '$user_email'";
$result = mysqli_query($dbc,$sql);
$num_match = mysqli_num_rows($result);


if (!$num_match) {
        echo "No result";
}
else{
    $sql = "SELECT * FROM member WHERE password = '$user_password' ";
    $result = mysqli_query($dbc,$sql);
    $password_match = mysqli_num_rows($result);
    if (!$password_match) {
        echo "SHA1 does not work";
        exit;
    }
    else{
        echo"success";
    }
}
}
else{
echo "You need to enter both Email and Password";
}
?>
HappyDayToday
  • 41
  • 2
  • 8
  • 1
    have you tried echo'ing the hashed database password and the one from the form just to check you actually are getting some content? its worth checking this to make sure the forms working ok, and that you have enough chars on the field in the db. – DevDonkey Feb 26 '15 at 10:04
  • Please look into your table member in the database, if there is the user under which you would like to login. BTW I am litte bit confused about your scripts name - action="try_login.php" and then login.php etc. – teo Feb 26 '15 at 10:29
  • 1
    **Danger**: SHA1 is [an unsuitable hashing algorithm](http://php.net/manual/en/faq.passwords.php); you need to [take better care](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) of your users' passwords. – Quentin Feb 26 '15 at 11:53
  • 2
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Feb 26 '15 at 11:53
  • 1
    In register.php you use the MySQL implementation of SHA1, while in login.php you use PHP's implementation. Have you checked they create the same representation of hash? (and BTW, using unsalted hashes is considered very poor security). And a single query should check BOTH the username and password. – symcbean Feb 26 '15 at 11:54
  • @MattHolbrook-Bull , Thanks Matt, I don't know how to echo'ing the hashed password from database. But I figured out what the problem was. I only set the password chars as 20. After I changed it to 100 it's working fine. – HappyDayToday Feb 26 '15 at 11:54
  • cool glad you sorted it. I dont know how many times Ive made that mistake myself! – DevDonkey Feb 26 '15 at 13:09

0 Answers0