PHP:错误的登录信息不会重定向到特定页面

时间:2010-04-22 13:18:13

标签: php mysql

<?php
include 'lib/db_conn.php';
$uid=$_REQUEST['uid'];
$pass=$_REQUEST['pass'];
if(($uid==NULL && $pass==NULL) ||($uid==NULL) ||($pass==NULL))
{
    header("location:index.php?msg=Fields can't be left blank..");
}

$pass=md5($pass);
$sql1="SELECT * FROM `tb_user` WHERE `email`='$uid' AND `pass`='$pass'";
$rs1=mysql_query($sql1) or die (mysql_error());
$row1=mysql_fetch_array($rs1) or die (mysql_error());
$email=$row1['email'];
if($uid==$email)
{
        session_start();
        $_SESSION['id']=$row1['id'];
        header("location:home.php");
}
else
{
header("location:index.php?msg=Wrong Credentials..");
}
?>

2 个答案:

答案 0 :(得分:0)

您应该使用代码标记。读起来很难看。

我认为您不应该按原样键入“错误的凭据...”,这些字母应该是URL编码的。此外,您应该在发送标题()-calls后退出()执行。

顺便说一下,你应该逃避$ uid,否则你可能会因为SQL注入而遇到麻烦。

答案 1 :(得分:0)

最好不要在地址栏中写一条消息,而只是将其标记,即:

header("location:index.php?msg=wcred");

并在index.php中:

if ($_GET['msg'] == "wcred") echo "Wrong Credentials..";

而且,正如Kai所说,$uid=$_REQUEST['uid'];必须是

$uid=mysql_real_escape_string($_REQUEST['uid']);

另外,正如dnagirl所说,现场空虚检查是错误的 另外,正如我要提到的,exit必须遵循任何位置标题

<?php
if((empty($_REQUEST['uid']) OR empty($_REQUEST['pass'])) {
    header("location:index.php?msg=fempty");
    exit;
}
include 'lib/db_conn.php';

$pass=md5($_REQUEST['pass']);
$pass=mysql_real_escape_string($pass);
$uid=mysql_real_escape_string($_REQUEST['uid']);

$sql1="SELECT * FROM `tb_user` WHERE `email`='$uid' AND `pass`='$pass'";
$rs1=mysql_query($sql1) or die (mysql_error());
$row1=mysql_fetch_array($rs1) or trigger_error(mysql_error());
if($row1) {
    session_start();
    $_SESSION['id']=$row1['id'];
    header("location:home.php");
    exit;
} else {
    header("location:index.php?msg=wcred");
    exit;
}
?>