Home » Php » php – check user name and password on database (script included)

php – check user name and password on database (script included)

Posted by: admin July 12, 2020 Leave a comment

Questions:

I posted my question here and before I edited the post it was closed as not a real question!

I have a login form like this:

<html>
  <head>
    <title>Password Checking Script</title>
  </head>
  <body>
    <form action="check_user-pass.php" method="POST">
      <h3>Please Login</h3>

      User Name: <input type="text" name="user_name"><br>
      Password: <input type="password" name="password">

      <input type="submit" name="submit" value="Login!">
    </form>
  </body>
</html>

As you see, this form authenticates the user through check_user-pass.php.
It looks for those credentials on my database; if they exist, returns OK, else returns value NO.

So my question is: exactly what code should I include in check_user-pass.php?
I tried to add more code but couldn’t do that as well! My current code is:

<?php
ob_start();
$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("$db_name") or die(mysql_error());
echo "Connected to Database<br />";
// Check $username and $password 
/*
if(empty($_POST['username']))
{
    echo "UserName/Password is empty!";
    return false;
}
if(empty($_POST['password']))
{
    echo "Password is empty!";
    return false;
}
*/

// Define $username and $password 
$username=$_POST['username']; 
$password=md5($_POST['pass']);


// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if ($count==1) {
    echo "Success! $count";
} else {
    echo "Unsuccessful! $count";
}

ob_end_flush();
?>
How to&Answers:

The name in you form is user_name but in your script you look for username

$username=$_POST['username']; 

should be

$username=$_POST['user_name']; 

EDIT:
If you use crypt to encrypt your password before you put them in the database, try this

$sql="SELECT * FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
    $row = mysql_fetch_assoc($result);
    if (crypt($password, $row['password']) == $row['password']){
        session_register("username");
        session_register("password"); 
        echo "Login Successful";
        return true;
    }
    else {
        echo "Wrong Username or Password";
        return false;
    }
}
else{
    echo "Wrong Username or Password";
    return false;
}

EDIT:
myBB seems to use a crapload of md5 hashing for their passwords, try this

$sql="SELECT * FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
    $row = mysql_fetch_assoc($result);
    if (md5(md5($row['salt']).md5($password)) == $row['password']){
        session_register("username");
        session_register("password"); 
        echo "Login Successful";
        return true;
    }
    else {
        echo "Wrong Username or Password";
        return false;
    }
}
else{
    echo "Wrong Username or Password";
    return false;
}

Also hashing is one way so you can’t get back the passwords already in the db, you’ll just have to get the users to change/update their passwords.
If the above works then you wouldn’t have to turn off encryption, and everything should be fine.

Answer:

Most times passwords in mysql databases are encrypted. If you entered them directly into the database, they may not have a salt. Try encrypting the password before you pass it into the query:

$crypted_pass = crypt($password);

If there’s a salt (often the first two letters of the username), pass that in with the crypt function:

$salt = substr($username, 0, 2);
$crypted_pass = crypt($password, $salt);

Answer:

Try something simple:

    if ($count==1) {
        echo "Success! $count";
    } else {
        echo "Unsuccessful! $count";
    }

This will see if you are returning anything greater than 1 or more values.