密码错误重定向到不正确的页面?

时间:2012-11-14 23:33:49

标签: login

所以我使用php和一个mysql数据库整理了一个非常粗略的登录表单,我设置(或者我认为)用“loginFailed = true& reason = password”“重定向回登录页面。”我试图让它重定向回登录,并显示错误的密码消息,但它只是重定向到主索引页。

我在这里做错了什么?当然,由于我缺乏编码知识,我从一些预先存在的代码中大量借用,但它在重定向之前确实按预期工作了。

以下是代码:

passwordcheck.php

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$password=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$password = stripslashes($password);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("password"); 
header("location:admin.html");
}

else {
die(header("location:login.html?loginFailed=true&reason=password"));
}
?>

这是登录页面中的密码字段:

<span class="add-on"><i class="icon-list-alt"></i></span>
<input type="password" id="inputIcon" class="span4" name='password' id='password' maxlength="50" placeholder="<?php $reasons = array("password" => "Yo shitbird, wrong password."); if ($_GET["loginFailed"]) echo $reasons[$_GET["reason"]]; ?>" />
</div>

1 个答案:

答案 0 :(得分:0)

尝试将header()命令移出die()来电:

else {
    header("location:login.html?loginFailed=true&reason=password");
    die();
}

此代码还有许多其他潜在问题,我建议您阅读有关该主题的一些教程there are plenty out there;虽然要小心,但有许多低质量的PHP教程可能会教你危险的做法。了解有关PHP security的更多信息非常重要,尤其是如果此代码将位于可公开访问的Web服务器上。

其中一个问题是您以纯文本格式存储密码。密码不应该以纯文本形式存储,应该加密并存储a secure hashing algorithmPHPass是一个很好的实用工具。

相关问题