如何在php中验证用户名和密码?

时间:2018-05-18 10:31:33

标签: php

我使用下面的php代码从数据库中获取数据。我无法检查用户名是否与输入的密码匹配。

输入正确的用户名和错误密码后,应用程序将被强行关闭。

Php代码 -

<?php

include 'DBConfig.php';

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

$json = file_get_contents('php://input');

$obj = json_decode($json,true);

$email = $obj['email'];

$password = $obj['password'];

$Sql_Query = "select * from UserRegistrationTable where email = '$email' and password = '$password'; ";

$connection_part = mysqli_query($con,$Sql_Query);
$dataExist=0;

if(mysqli_num_rows($connection_part)>0){ 

 $SuccessLoginMsg = 'Data Matched';

 $SuccessLoginJson = json_encode($SuccessLoginMsg);

echo $SuccessLoginJson ; 

$check = mysqli_fetch_array($connection_part);

$response=array();
array_push($response,array("name"=>$check[1], "email"=>$check[2], "password"=>$check[3]);

echo json_encode(array("Details"=>$response));     
}

 else{

$InvalidMSG = 'Invalid Username or Password Please Try Again' ;

$InvalidMSGJSon = json_encode($InvalidMSG);

echo $InvalidMSGJSon ;

 }

 mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:0)

您应该切换if语句或更好地更改'&gt;'到'&lt;'。如果您的SQL查询中有行意味着用户名和密码匹配。你有另一种希望,这有助于它。