简体   繁体   English

PHP登录脚本无法正常工作

[英]PHP Login Script Not Working Properly

I am trying to make a login form with php and mysql and failed from past 3 days. 我试图用php和mysql创建一个登录表单,并在过去的3天内失败了。

I have basically php form and on the same page the scripting for logging the user into the website. 我基本上有PHP表单,并在同一页面上用于将用户登录到网站的脚本。

form.php form.php的

<form id="superAdminForm" method="post" action="">
   <input type="email" name="email" required class="txtInput" placeholder="Email..." autocomplete="off"/> <br />
   <input type="password" name="password" required class="txtInput" placeholder="Password..."/> <br />
   <input type="submit" name="submit" value="Enter" id="submit" />
</form>

php code for form.php form.php的php代码

<?php
    require("../php_includes/db-connect.php");

    if (isset($_POST["submit"])) {
        $email = $_POST["email"];
        $password = $_POST["password"];

        $sql = "SELECT * FROM users WHERE email='$email' AND password='$password' LIMIT 1";
        $query = mysqli_query($con, $sql);

        $row = mysqli_fetch_array($query);

        if ($row['email'] == 1) {
            header("Location: admin-index.php");
        }
    }

    mysqli_close($con);

?>

The problem is that the login is not getting successfull. 问题是登录没有成功。 I don't know the reason behind it. 我不知道背后的原因。 So please help me in this. 所以请帮助我。 Also guide me where I am making mistakes. 还指导我在哪里犯错误。

I am new in PHP and am trying my best to cope up with my knowledge. 我是PHP的新手,我正在尽我所能来应对我的知识。

I think that what you need to do is to separate your view logic from your authentication logic. 认为您需要做的是将您的视图逻辑与您的身份验证逻辑分开。 Move the PHP block into its own file, and set the action parameter of your form to that file (relative location). 将PHP块移动到自己的文件中,并将表单的action参数设置为该文件(相对位置)。

This is because all of that php code is interpreted by the server before being sent to the client, and so this form can't actually utilize that PHP logic in the same page. 这是因为所有这些php代码被发送到客户端之前都被服务器解释,因此这个表单实际上不能在同一页面中使用该PHP逻辑。

You'll also probably want to write a fallback in case authentication fails that will redirect you back to the login page (or wherever you'd like), otherwise the user will be left with a white screen. 如果身份验证失败会将您重定向回登录页面(或您想要的任何地方),您可能还想写一个后备,否则用户将留下白屏。

EDIT 编辑

As stated by others, you should look into security and especially hooking into $_SESSION s. 正如其他人所说,你应该研究安全性,尤其是挂钩$_SESSION Your code is also going to need some work. 您的代码也需要一些工作。 I don't think $row['email'] == 1 will ever be true, and so your script will fail and ultimately do nothing. 我不认为$row['email'] == 1将是真的,所以你的脚本会失败并最终什么都不做。 Again, add an else statement to handle authentication failure. 再次,添加else语句来处理身份验证失败。

As far as a fix, I'd suggest comparing $row['email'] == $email in the if, but that would also make the email check in your SQL query redundant. 至于修复,我建议在if中比较$row['email'] == $email ,但这也会使您的SQL查询中的电子邮件检查变得多余。 I'd suggest restructuring your logic. 我建议重组你的逻辑。

1.. Talking about security 1 ..谈论安全问题

your code is vulnerable to sql injection so even if it will work its not useful 你的代码很容易被sql注入,所以即使它会起作用也没用

在此输入图像描述

^^ source ^^来源

' or '1'='1' /* ' will do magic ' or '1'='1' /* '会做魔术

so either use mysqli_real_escape_string function For manually escaping special characters in string or Use prepared statements and parameterized queries (Recommended). 所以要么使用mysqli_real_escape_string函数用于手动转义字符串中的特殊字符,要么使用 准备语句和参数化查询 (推荐)。

2.. email='$email' AND password='$password' LIMIT 1"; 2 .. email='$email' AND password='$password' LIMIT 1";

so why not email should be unique or check when login that email is already exist or not and if exist than show the error message like (Email already exist) 那么为什么电子邮件不应该是唯一的,或者在登录时检查电子邮件是否已经存在,如果存在,则显示错误消息,如(电子邮件已经存在)

so you wont need to use limit 1 which really makes no sense 所以你不需要使用limit 1 ,这真的没有意义

3.. Why are you not setting login id or something in SESSION so that you can determine that use is logged in 3 ..为什么不在SESSION设置登录ID或其他内容,以便您可以确定已登录使用

your above login script does nothing it just check weather the email and password is present in database or not 你上面的登录脚本什么也不做,它只是检查天气,电子邮件和密码是否存在于数据库中

4.. plan text as password is really bad idea instead use Hashing check this How do you use bcrypt for hashing passwords in PHP? 4 .. 计划文本作为密码是非常糟糕的想法而不是使用哈希检查这个如何在PHP中使用bcrypt进行哈希密码?

Good Read 好读

  1. How can I prevent SQL injection in PHP? 如何在PHP中阻止SQL注入?
rows=mysqli_num_rows( $query);
if ($rows == 1) {
     header("Location: admin-index.php");
}

You are comparing an array to an int,use the code above. 您正在将数组与int进行比较,请使用上面的代码。

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

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