When I go to Localhost and try the username and th

2019-06-10 06:25发布

问题:

When I go to Localhost and try the username and the password it tells me that is an error? why?

回答1:

It looks entered username/password or both are not matching with the database table my_db.

Reasons : Localhost means the db is getting refereed in local. So in your local database under my_db table there is no user exists with the given username and password. May be that data is valid in remote database server.

Solution : 1. Take a dump from remote database and put in your localhost so that your local db and remote db are replica 2. For simple solution, just insert the required details in my_db table.

Hope that helps



回答2:

1) Use password_hash docs

while registration use password_hash() to hash the password and store it in database and while login use password_verify() to verify the password like this .

2) user prepared statement to avoid sql injection

<?php
   session_start();
  if(isset($_SESSION["user_id"])){
    header("location: /web/home.php");
   }
if(isset($_POST["s"])){
   $username = $_POST["un"];
   $password = $_POST["ps"];

 $conn = mysqli_connect("localhost","root","","my_db") or die("Connection failed: " . mysqli_connect_error());


$stmt = $conn->prepare("SELECT * FROM table_name WHERE username=?");

$stmt->bind_param('s',$username);

$stmt->execute();
$get_result =$stmt->get_result();

$row_count= $get_result->num_rows;

if($row_count>0)
{
    $record = $get_result->fetch_assoc();
    if(password_verify($password,$record['password']))
    {
    $_SESSION["user_id"]= $record["user_id"];
    header("location: /web/home.php");
    }
    else
    {
      echo "<h3 style = 'color:red'>Error in username or password</h3>";
    }

}else{
    echo "<h3 style = 'color:red'>Error in username or password</h3>";
}
 }
?>