表格提交后直接重定向

时间:2014-01-03 21:35:56

标签: php

<?php
    require("../inc/global.inc.php");

    if(!empty($_POST["username"])) {
        $username = $_POST["username"];
        $password = $_POST["password"];
        $checkuser_query = mysqli_query($con,"SELECT * FROM users WHERE username='$username' AND password='$password'");
        $checkuser = mysqli_num_rows($checkuser_query);

        if($checkuser>0) {
            die("<div align='center'><div class='indexbody'><h1>Error Message</h1>We couldn't log you in! Press your browsers back button to fix it.<p />");
        }
        else {
            header("Location: http://empirebattles.co.uk/Home");
            setcookie("empirebattles",$username,time()+60*60*24*30*365*50);
        }
    }
?>

我在提交loginform时运行此代码。但是,即使登录详细信息错误,它也会重定向我。知道为什么??

4 个答案:

答案 0 :(得分:2)

你的逻辑是落后的:

if($checkuser>0) { // reads "if a user was found"
    // Dies saying "can't log you in"
}
else { // No user is found
    // Redirect
}

答案 1 :(得分:1)

if($checkuser>0) {
            die("<div align='center'><div class='indexbody'><h1>Error Message</h1>We couldn't log you in! Press your browsers back button to fix it.<p />");
        }
        else {
            header("Location: http://empirebattles.co.uk/Home");
            setcookie("empirebattles",$username,time()+60*60*24*30*365*50);
        }

将其更改为:

if($checkuser<=0) {
            die("<div align='center'><div class='indexbody'><h1>Error Message</h1>We couldn't log you in! Press your browsers back button to fix it.<p />");
        }
        else {
            header("Location: http://empirebattles.co.uk/Home");
            setcookie("empirebattles",$username,time()+60*60*24*30*365*50);
        }

答案 2 :(得分:0)

您需要更改if:

的条件
if($checkuser==0) {
   die("<div align='center'><div class='indexbody'><h1>Error Message</h1>We couldn't log you in! Press your browsers back button to fix it.<p />");
} else {
   header("Location: http://empirebattles.co.uk/Home");
   setcookie("empirebattles",$username,time()+60*60*24*30*365*50);
}

答案 3 :(得分:0)

试试这个

<?php
    require("../inc/global.inc.php");

    if(!empty($_POST["username"])) {
        $username = $_POST["username"];
        $password = $_POST["password"];
        $checkuser_query = mysqli_query($con,"SELECT * FROM users WHERE username='$username' AND password='$password'");
        $checkuser = mysqli_num_rows($checkuser_query);

        if($checkuser>0) {
            header("Location: http://empirebattles.co.uk/Home");
            setcookie("empirebattles",$username,time()+60*60*24*30*365*50);

        }
        else {
             die("<div align='center'><div class='indexbody'><h1>Error Message</h1>We couldn't log you in! Press your browsers back button to fix it.<p />");
        }
    }
?>