简体   繁体   English

php / html5登录验证

[英]php/html5 login authentication

I am trying to authenticate a user in a MYsql database. 我正在尝试在MYsql数据库中验证用户。 I have tried so many tutorials to just fail every time. 我尝试了很多教程,每次都失败了。 I am pasting my code below, I dont care about security or sql injection right now. 我正在粘贴下面的代码,我现在不关心安全性或sql注入。 I am just trying to get this to work so I can go on to my next requirement. 我只是想让它工作,所以我可以继续我的下一个要求。 If anybody has any links to a GOOD php/html5 login authentication tutorial then please share. 如果有人有任何链接到GOOD php / html5登录认证教程,那么请分享。 I easily was able to create a registration form but this login form is really giving me difficulties. 我很容易就能创建一个注册表单,但是这个登录表单确实给了我很多困难。

$id =$_POST['Id'];
$password =$_POST['password'];
$accessdb = "SELECT * from UserData where Id ='$id' and password ='$password";
            $authenticate = mysqli_query($conn, $accessdb);
                    if (mysqli_num_rows($authenticate) == 1) {
                       session_start();
                        $_SESSION['auth'] = 'true';
                        header('location: Profile.php');
                    }
                    else {
                        echo "Wrong log-in credentials";
                    }

First of all, session_start() should be the very first thing on your page and should be on all the pages you want your user authenticated to get access to. 首先, session_start()应该是页面上的第一件事,应该在您希望用户通过身份验证的所有页面上进行访问。

Then, you need to correct your query $accessdb like so: 然后,您需要更正您的查询$accessdb如下所示:

$accessdb = "SELECT * FROM userdata WHERE id = {$id} AND password ='{$password}'";

NB : not sure if $id is a number. 注意 :不确定$id是否为数字。 If it's not, then do: '{$id}' instead of {$id} . 如果不是,请执行: '{$id}'而不是{$id}

        $password =$_POST['password'];
        $user = "SELECT * FROM UserData WHERE Id ='$id'";
        $authenticate = $conn->query($user);
        $row = $authenticate->fetch_assoc();
        $hash_password = $row['password'];
        $hash = password_verify($password, $hash_password);
        if ($hash ==0) {
            echo "Something went wrong!!";
            header('Location: LogIn.php');
            exit();
        }
        else {
       `enter code here`$accessdb = "SELECT * FROM UserData WHERE Id ='$id'                                                      
        AND    password = '$hash_password'";
            $authenticate = $conn->query($accessdb);
                if (!$row = $authenticate->fetch_assoc()) {
                     echo "Your username is incorrect, Re-enter CREDENTIALS";
            }
                 else {
                     $_SESSION['id'] = $id;
                     header('Location: Profile.php');
                }
        }

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

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