如何解决我的登录问题?

时间:2018-06-07 09:45:06

标签: php mysqli login error-handling

我已成功创建了一个注册表单,但它运行正常。但我的登录系统显示错误,我无法弄清楚。每当我使用登录时,即使我使用完全相同的用户和密码,它也会在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();
 }

1 个答案:

答案 0 :(得分:-1)

三件事。

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

$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

相关问题