首页 > 解决方案 > 如何解决我的登录问题?

问题描述

我已经成功创建了一个注册表单,它工作正常。但是我的登录系统显示错误,我无法弄清楚。每当我使用登录时,即使我使用完全相同的用户和密码,它也会在 URL 中显示 login==empty。

<?php
session_start();
include 'conn.php';
 if (isset($_POST['submit'])) {

      $uid=mysqli_real_escape_string($conn,'$_POST["firstname"]');
      $pwd=mysqli_real_escape_string($conn,'$_POST["password"]');
      if ($uid==""||$pwd=="") {
         header("Location:../triallogin.php?login=empty");
            exit();
      } else{
        $sql="SELECT * FROM comments WHERE first='$uid'";
        $result=mysqli_query($conn,$sql);
        $resultcheck=mysqli_num_rows($result);
         if ($resultcheck<1) {
            header("Location:../triallogin.php?login=empty");
            exit();
         } else{
            if ($row=mysqli_fetch_assoc($result)) {
                //De-hasing of password
                $hashedpassowrd=password_verify($pwd,$row['pwd']);
                if ($hashedpassowrd==false) {
                    header("Location:../triallogin?login=wrongpassword");
                } elseif ($hashedpassowrd==true) {
                    $_SESSION['first']=$row['first'];
                    $_SESSION['last']=$row['last'];
                    header('Location:../triallogin.php?login=success');
                    exit();
                }
            }


         }  
      }
 } else {
    header('Location:../triallogin.php?login=hit');
    exit();
 }

标签: phpmysqliloginerror-handling

解决方案


三件事。

您已将帖子变量作为字符串:

$uid=mysqli_real_escape_string($conn,'$_POST["firstname"]');
$pwd=mysqli_real_escape_string($conn,'$_POST["password"]');

将其更改为:

$uid = mysqli_real_escape_string($conn, $_POST["firstname"]);
$pwd = mysqli_real_escape_string($conn, $_POST["password"]);

其次,您正在尝试从comments表中获取您的用户登录详细信息?这不可能。

$sql="SELECT * FROM comments WHERE first='$uid'";

可能将其更改为users表格,或任何您称之为的内容。

最后,您的 SQL 容易受到 SQL 注入攻击。解决方法是使用准备好的语句并绑定参数。

我建议在 mysqli 上使用 PDO,这可能是解释原因的最佳文章之一,请阅读!https://phpdelusions.net/pdo


推荐阅读