PHP网页有直接循环错误?

时间:2013-05-23 16:41:54

标签: php login

我一直在尝试在PHP中创建一个登录页面无济于事,我在尝试运行时遇到“此网页有重定向循环”错误,所以我想知道是否有人可能发现我的错误?这是我的代码:

<?php
require_once("nocache.php");
$id = $_POST["id"];
$pword = $_POST["pword"];
if (!empty($_POST){
if (!empty($id) || !empty($pword)){
require_once("dbconn.php");
 $sql = "select username, school_type from school_info where username = '$id' and password = '$pword'";
 $rs = mysql_query($sql, $dbConn);
 if (mysql_num_rows($rs)> 0 ) {
     session_start();
     $_SESSION["who"] = $id;
     $_SESSION["school_type"] = mysql_result($rs,0,"school_type");
     header("location: EOI_home.php");
    }
}
else {
header("location: login.php");}     
} else {
   header("location: login.php");}

?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="login">

ID: <input type="text" name="id" /><br/>
pword: <input type="password" name="pword" /><br/>

<input type="submit" value="log in" />&nbsp;
<input type="reset" />

</form>

2 个答案:

答案 0 :(得分:1)

所有给出的答案在技术上都是正确的,您设置逻辑的方式不正确...请参考以下示例并将其移植到您自己的代码中。

<?php

$id = $_POST["id"];
$pword = $_POST["pword"];

if(!empty($_POST)) {
    // The form was submitted, perform validation here.

    if(!empty($id) || !empty($pword)) {
        // Form validation passed, insert into database
    } else {
        // Form validation failed, display an error or redirect back
    }
} else {
    // Form was not submitted, so display the form.
}

?>

修改 我希望不要为你做这项工作(因为你学习的最好),但也许看到上面的代码,以及下面的代码,你可以从中学习吗?

<?php
require_once("nocache.php");

$id = $_POST["id"];
$pword = $_POST["pword"];

if(!empty($_POST)) {
    if(!empty($id) || !empty($pword)) {
        require_once("dbconn.php");
        $sql = "select username, school_type from school_info where username = '$id' and password = '$pword'";

        $rs = mysql_query($sql, $dbConn);

        if(mysql_num_rows($rs) > 0) {
            session_start();
            $_SESSION["who"] = $id;
            $_SESSION["school_type"] = mysql_result($rs, 0, "school_type");

            header("location: EOI_home.php");
        }
    } else {
        header("location: login.php");
    }
}
?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="login">
    ID: <input type="text" name="id" /><br/>
    pword: <input type="password" name="pword" /><br/>

    <input type="submit" value="log in" />&nbsp;
    <input type="reset" />
</form>

答案 1 :(得分:0)

循环在这里:

$id = $_POST["id"];
$pword = $_POST["pword"];
if (empty($id) || empty($pword)){
    header("location: login.php"); }

如果未设置$_POST值,则会发生重定向。由于您没有在重定向之前设置值,因此会再次发生。然后再次。再次......

要解决此问题,请在未设置$_POST值的情况下显示表单。