简体   繁体   English

PHP中的用户名和密码问题

[英]Username and Password trouble in PHP

I am trying to create a login form with PHP but it cannot login. 我正在尝试使用PHP创建登录表单,但无法登录。 I get incorrect password/username instead. 我得到的密码/用户名不正确。 I have triple checked the credentials in the database and they are correct. 我已经三重检查了数据库中的凭据,它们是正确的。 Please help 请帮忙

Here is the code 这是代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
     require('db.php');
     session_start();
     // If form submitted, insert values into the database.
     if (isset($_POST['username'])){
         $username = $_POST['username'];
         $password = $_POST['password'];
         $username = stripslashes($username);
         $username = mysqli_real_escape_string($connection,$username);
         $password = stripslashes($password);
         $password = mysqli_real_escape_string($connection,$password);
         //Checking is user existing in the database or not
         $query = "SELECT * FROM `backuser` WHERE username='".$username."' and password='".md5($password)."'";
         $result = $connection->query($query) or die(mysqli_error());
         $rows = mysqli_num_rows($result);
         if($rows==0){
             $_SESSION['username'] = $username;
             header("Location: index.php"); // Redirect user to index.php
         }else{
             echo " <div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
         }
     }else{
?>
<div class="form">
<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
</form>
</div>
<?php } ?>
</body>
</html>

You have an error in your logic. 您的逻辑有误。 Change if($rows==0) to if($rows!=0) . if($rows==0)更改为if($rows!=0) If the user is in DB, you should have at least 1 record returned and mysqli_num_rows() should return a number >= 1 . 如果用户在DB中,则应至少返回1条记录,并且mysqli_num_rows()应返回一个数字>= 1

Sarasvati's answer appears to fix your immediate issue, however I would like to also suggest: 萨拉斯瓦蒂(Sarasvati)的答案似乎可以解决您眼前的问题,但是我也建议:

  • Use PHP Password hashing built in functions (PHP 5.5+ and 5.3) for hashing passwords. 使用内置的PHP Password hashing函数(PHP 5.5+和5.3)来哈希密码。 MD5 is not cyptographically secure and is not advised for password storage or checking. MD5在密码学上不安全,不建议用于密码存储或检查。

  • If you are new to PHP it is very very much worth your time exploring using Prepared Statements with your code. 如果你是新的PHP是非常非常值得你花时间探索使用预处理语句与您的代码。 This seperates code from user input and so greatly reduces the risk of SQL injection and Database Compromise . 这样可以将codeuser input分开,从而大大降低了SQL注入和数据库破坏的风险。

  • When you have a header which redirects you should be immediately following this header with an exit statement, as the rest of the PHP code will still be run before the header is followed. 当您有一个重定向的header ,应立即在此标头后面加上一个exit语句,因为其余的PHP代码仍将在标头跟随之前运行。

      $_SESSION['username'] = $username; header("Location: index.php"); // Redirect user to index.php exit; 
  • Do not use text editing functions such as stripslashes() on password fields, if someone has a slash in their password, it will be stripped and they will not be able to log in. Passwords should be any character, not just az or 0-9 etc. 不要在密码字段上使用诸如stripslashes()类的文本编辑功能,如果某人的密码中有斜杠,它将被剥夺并且他们将无法登录。密码应为任何字符,而不仅仅是z或0- 9等

  • For MySQLi_ [procedural] error reporting you need to provide the connection details: 对于MySQLi_ [procedural]错误报告,您需要提供连接详细信息:

     die(mysqli_error($connection)); 

    otherwise the report will not give you anything. 否则,报告将不会给您任何东西。

  • PHP session_start() should be before anything is output to the browser, so this needs to be at the very top of your PHP page. PHP session_start()应该任何内容输出到浏览器之前,因此它必须在PHP页面的顶部。 ( Thanks to Nitin for this, I missed this originally ) . 感谢Nitin,我本来想念这个的 )。

The problem is that i had not allocated enough storage(character length) in my database to store the hash value. 问题是我没有在数据库中分配足够的存储(字符长度)来存储哈希值。 I adjusted the length value to 30 and it works like a charm. 我将长度值调整为30,它就像一个吊饰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM