【发布时间】:2016-09-13 06:01:02
【问题描述】:
所以我一直在寻找诸如“随机重定向回 index.php”和“表单未正确提交”之类的问题,但似乎没有一个问题能解决我的问题,所以我想也许我的问题是一个特定的问题。 (可能包含多个错误)。
基本上我有一个登录页面,如果用户名和密码与数据库中的任何一个都不匹配,我应该重定向到 register.php,如果用户名(电子邮件)和密码都匹配,则重定向到 index.php。
登录.php
<?php
include "header.php";
?>
<!--follow from header.php-->
<div class="ui centered grid" id="loginbox">
<div class="ui inverted segment">
<form class="ui inverted form" action="login.php" method="post" enctype="multipart/form-data">
<div class="two fields">
<div class="required field">
<label>Username</label>
<input placeholder="Username" type="text" name="log_email" required/>
</div>
<div class="required field">
<label>Password</label>
<input placeholder="Password" type="text" name="log_password" required/>
</div>
</div>
<button class="ui submit button" name="login" type="submit"><a href="index.php">Log In</a></button>
<button class="ui button"><a href="register.php">Register</a> </button>
</form>
</div>
</div>
<!--continue to footer_form.php-->
<?php
if(isset($_POST['login'])){
$log_email = $_POST['log_email'];
$log_password = $_POST['log_password'];
$sel_log = "select * from customers where email ='$log_email' and password ='$log_password'";
$run_log = mysqli($mysqli,$sel_log);
$check_customer = mysqli_num_rows($run_log);
if($check_customer==0){
echo"<script>alert('Please register first!')</script>";
header("Location: register.php");
exit;
}
if($check_customer>0){
echo"<script>alert('Logged in successfully!')</script>";
header("Location: index.php");
exit;
}
}
include "footer_form.php" ?>
TLDR; 我的问题是: 1)警报脚本似乎不适用于两种 IF 情况 2) 偶尔在login.php页面点击“登录”,一直停留在同一个页面,有时到index.php,很不一致,不管你输入的是什么用户名密码。
数据库端所有命名和连接都是正确的。
编辑: php表单问题已经被下方评论区的朋友解决了。我只是想和你分享一个解决脚本问题的答案。
if($check_customer==0){
echo"<script>alert('Please register first!');window.location='register.php'</script>";
exit;
}
使用上面的代码解决了这个问题,因为它似乎警报脚本和标题不兼容,使用 window.location 很好地解决了它。
【问题讨论】:
-
您用于检查登录的 SQL 查询对 SQL 注入开放,并且您似乎在存储未加密的密码。请使用准备好的语句和绑定参数来降低 SQL 注入风险:secure.php.net/manual/en/…,并使用 php 的密码函数来保护密码:secure.php.net/manual/en/function.password-hash.php。考虑通读 PHP The Right Way,这是一个关于常见陷阱以及如何避免不良做法的良好指南:phptherightway.com
-
@jedifans 您好,感谢您的评论!将查找设置登录系统的正确方法。现在我只想知道为什么这不起作用,尽管不安全。谢谢!
标签: javascript php html forms alert