Home  >  Article  >  Backend Development  >  A small problem with PHP about login

A small problem with PHP about login

WBOY
WBOYOriginal
2016-10-10 11:56:001004browse

<code>if($row){
   //判断密码是否正确
   if($row['user_password']===$password){
       echo 1;
   }else{
       echo "密码错误"; 
   }
}else{
   echo "没找到用户名";    
}
</code>

The above code is used to verify whether the user name and password entered by the user are correct. For the sake of convenience, I have simplified the code and removed the content of obtaining database data. Why does it output "User not found" when nothing is entered after opening the page?
A small problem with PHP about login

Reply content:

<code>if($row){
   //判断密码是否正确
   if($row['user_password']===$password){
       echo 1;
   }else{
       echo "密码错误"; 
   }
}else{
   echo "没找到用户名";    
}
</code>

The above code is used to verify whether the user name and password entered by the user are correct. For the sake of convenience, I have simplified the code and removed the content of obtaining database data. Why does it output "User not found" when nothing is entered after opening the page?
A small problem with PHP about login

if($row) This piece of code needs to be written in the POST request code block. In this way, this code will not be executed when getting.

For example, after clicking login, the submitted user and password fields are: username password

<code>$username = isset($_POST['username']) ? $_POST['username'] : false;
$password = isset($_POST['password']) ? $_POST['password'] : false;

if($username && $password){
    if($row){
       //判断密码是否正确
       if($row['user_password']===$password){
           echo 1;
       }else{
           echo "密码错误"; 
       }
    }else{
       echo "没找到用户名";    
    }
}
</code>

Print out $row to see what it is

That means this code is executed when you open the webpage, and the if judgment is false

Since the user is not found after printing, it means $row is false, then you should focus on the code to obtain $row instead of focusing on the posted code

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn