Php 如何在MySQL数据库中使用凭据对用户进行身份验证

Php 如何在MySQL数据库中使用凭据对用户进行身份验证,php,html,mysql,authentication,Php,Html,Mysql,Authentication,在我的表单页面上,我有两个文本框,分别是name和password 当用户点击submit时,它将数据发送到MySQL数据库中名为“name”和“password”的两列中 记录数据后(这是我理解的部分,不需要帮助),我希望用户在登录页面上输入他的/她的姓名和密码,并且只有在数据库中已经存在姓名和密码数据(我不理解的部分)的情况下,才允许用户进入站点 我是否会使用以下查询: SELECT * FROM tablename WHERE name & password = "'$_POST[

在我的表单页面上,我有两个文本框,分别是
name
password

当用户点击submit时,它将数据发送到MySQL数据库中名为“name”和“password”的两列中

记录数据后(这是我理解的部分,不需要帮助),我希望用户在登录页面上输入他的/她的姓名和密码,并且只有在数据库中已经存在姓名和密码数据(我不理解的部分)的情况下,才允许用户进入站点

我是否会使用以下查询:

SELECT * FROM tablename WHERE name & password = "'$_POST[name]', $_POST[password]'

您应该使用
&&
而不仅仅是一个符号(
&
),并将要绑定的变量与其列名分开

还应该考虑在使用变量之前对变量进行消毒。你可以用它来预防

但我能给你的最好建议是使用mysql,而不是
mysql

if($stmt = $con->prepare("SELECT * FROM tablename WHERE name = ? AND password = ?")){ /* PREPARE THE QUERY; $con SHOULD BE ESTABLISHED FIRST USING ALSO mysqli */
  $stmt->bind_param("ss",$_POST["name"],$_POST["password"]); /* BIND THESE VARIABLES TO YOUR QUERY; s STANDS FOR STRINGS */
  $stmt->execute(); /* EXECUTE THE QUERY */
  $noofrows = $stmt->num_rows; /* STORE THE NUMBER OF ROW RESULTS */
  $stmt->close();  /* CLOSE THE STATEMENT */
} /* CLOSE THE PREPARED STATEMENT */

要保护密码,您还可以查看。

您可以使用


从tablename中选择count(*),其中name=“”。$\u POST[name]”和password=“”$_张贴[密码]。”

您应该期望计数正好为1-表示有效用户,0-表示无效用户


任何大于1的值都应该是无效的,这表明数据库中存在某种不一致性…

您应该随后将变量分配给name&pass

您可以尝试以下方法:

$con = mysqli_connect("localhost","YOURUSER","YOURPASS","YOURDB");
if (mysqli_connect_errno())

{
    echo"The Connection was not established" . mysqli_connect_error();



$user
= mysqli_real_escape_string($con,$_POST['user']);

$pass = mysqli_real_escape_string($con,$_POST['password']);

$query = "select * from tablename where user ='$user' AND password='$pass' ";
$run = mysqli_query($con,$query);
$check = mysqli_num_rows($run );
            if($check == 0)
            {
                echo "<script> alert('Password or Email is wrong,try again!')</script>";

            }
            else 
            {
    //get a session for user    

$_SESSION['user']=$user;

                  // head to index.php; you can just put index.php if you like

                echo"<script>window.open('index.php?login=Welcome to Admin Area!','_self')</script>";
            }
$con=mysqli_connect(“本地主机”、“您的用户”、“您的通行证”、“您的数据库”);
if(mysqli\u connect\u errno())
{
echo“连接未建立”。mysqli_connect_error();
$user
=mysqli_real_escape_字符串($con,$_POST['user']);
$pass=mysqli\u real\u escape\u字符串($con,$\u POST['password']);
$query=“从tablename中选择*,其中user='$user'和password='$pass';
$run=mysqli\u查询($con,$query);
$check=mysqli\u num\u行($run);
如果($check==0)
{
回显“警报('密码或电子邮件错误,请重试!'”);
}
其他的
{
//为用户获取会话
$\会话['user']=$user;
//转到index.php;如果愿意,您可以直接放入index.php
echo“window.open('index.php?login=Welcome to Admin Area!”,“u self”);
}

< /代码> < P>请强>始终>准备>语句< /强>以执行来自代码外的变量的SQL代码。将变量从用户输入连接到SQL代码是危险的(考虑SQL注入),可以使用“强”>准备好的语句<或强>或(推荐)。< /P> Mysqli示例:

$mysqli = new mysqli("example.com", "user", "password", "database");
// error check you connection here
$query='select * from tablename where user =? AND password=?';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ss", $user,$password);
$stmt->execute();
if($stmt->num_rows!=1) {
   // check failed
}else{
   // check success
}
PDO示例(推荐)


另外,您还应该考虑使用某种形式的“强> 1路密码加密< <强>”(密码哈希),然后将其存储在数据库中,并将其与哈希(最常用的方式是使用)进行比较。.

使用php的
mysqli_num_rows
如果您使用过mysqli_*APIWarning:。同意……如果问题想了解高级级别和安全性。有很多类似于https、通过网络和数据库加密密码、SQL注入等。。。。
$con = mysqli_connect("localhost","YOURUSER","YOURPASS","YOURDB");
if (mysqli_connect_errno())

{
    echo"The Connection was not established" . mysqli_connect_error();



$user
= mysqli_real_escape_string($con,$_POST['user']);

$pass = mysqli_real_escape_string($con,$_POST['password']);

$query = "select * from tablename where user ='$user' AND password='$pass' ";
$run = mysqli_query($con,$query);
$check = mysqli_num_rows($run );
            if($check == 0)
            {
                echo "<script> alert('Password or Email is wrong,try again!')</script>";

            }
            else 
            {
    //get a session for user    

$_SESSION['user']=$user;

                  // head to index.php; you can just put index.php if you like

                echo"<script>window.open('index.php?login=Welcome to Admin Area!','_self')</script>";
            }
$mysqli = new mysqli("example.com", "user", "password", "database");
// error check you connection here
$query='select * from tablename where user =? AND password=?';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ss", $user,$password);
$stmt->execute();
if($stmt->num_rows!=1) {
   // check failed
}else{
   // check success
}
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// error check you connection here
$query='select * from tablename where user =? AND password=?';
$stmt = $dbh->prepare($query);
$stmt->bindParam(1,$user);
$stmt->bindParam(2,$password);
$stmt->execute();
if($sth->fetchAll()) {
    // check success
}else{
    // check failure
}