php - 使用Mysql数据库用户名和密码登录

标签 php mysql

<分区>

我正在尝试使我的 php 脚本 protected 页面,因此在添加登录名时我希望它根据数据库用户名和密码检查用户名和密码输入,如果它们匹配,它将允许他们查看 protected 页面直到注销。我已经能够根据虚拟登录用户名和密码设置 protected 页面和注销,现在我想将它连接到数据库用户。这是我的 html 和 php 代码。任何帮助将不胜感激:)

HTML:

<html>
<head>
<title>Login</title>

</head>
<body>
<form method="post" action="login.php">
 <table border="1" cellpadding="2">
<caption>
Staff Login
</caption>
<tr>
    <td><label for="username">Username</label></td>
    <td><input type="text" name="username" cols="45" rows="5" tabindex="2"/></td>
</tr>
    <tr>
    <td><label for="password">Password</label></td>
    <td><input type="password" name="password" cols="45" rows="5" tabindex="2"></textarea></td>
</tr>
<tr>
    <td colspan="2" align="center"><input type="submit" value="Login" /></td>
</tr>
</table>

</form>
</body>
</html>

PHP:

<html>
<head>
<title>Login</title>
</head>
<body>

<?php

// Check if username and password are correct


if ($_POST["username"] == 'username' && $_POST["password"] == 'password') {
include 'connection.php'; 
// If correct, we set the session to YES
  session_start();
  $_SESSION["Login"] = "YES";
  echo "You are now logged in.</br></br>";
  echo "<a href='list-contacts.php'>Proceed to contacts...</a>";

}
else {

// If not correct, we set the session to NO
  session_start();
  $_SESSION["Login"] = "NO";
  echo "Incorrect username and / or password.</br></br>";
  echo "<a href='login.html'>Return to login</a>";

}

?>

</body>
</html>

最佳答案

<?php session_start();
    include('../db.php');
    if(isset($_POST['login_submit'])){

        if(!empty($_POST['user_name']) && !empty($_POST['user_password'])){
            $get_username=mysqli_real_escape_string($conn,$_POST['user_name']);
            $get_password=mysqli_real_escape_string($conn,$_POST['user_password']);
            $sql="SELECT * FROM users WHERE user_name= '$get_username' AND user_password = '$get_password' ";

            if($result=mysqli_query($conn,$sql)){

                if(mysqli_num_rows($result)==1){
                    header('Location:../admin/index.php');
                }else{
                    header('Location:../index.php?login=wrong');
                }               
            }else{
                header('Location:../index.php?login=error');
            }               
        }else{
            header('Location:../index.php?login=empty');
            }       
    }else{}

?>

这是一个 php 5 mysqli_num_row() 函数,用于从 db.. 中验证用户名和密码

关于php - 使用Mysql数据库用户名和密码登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847491/

相关文章:

php - 实现接口(interface)的类必须在子类之前声明?

使用 2 个 mysqld(API) 的 Mysql 集群每次访问都会产生不同的表

php - 通过过滤器从多个数据库中选择

mysql - 如何查询超过 2 列的计数

php - 计算两个时间之间的差异()

php - 同时考虑安全性来保存用户输入

php - 在 PHP 中根据 SSHA256 哈希验证密码

python - 异常 'module has no attribute connector' - Python mysql

php - MSSQL Server INSERT NULL,但在 DATETIME 中插入 GetDate()

php - 防止直接访问 mp3/wav 文件,同时允许 Flash 播放器使用 .htaccess(或 PHP)访问它们