首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php重定向循环

php重定向循环
EN

Stack Overflow用户
提问于 2011-03-10 18:59:45
回答 3查看 10.9K关注 0票数 0

我正在关注一个来自Lynda.com的php视频教程(不是在线的对不起),并使用了以下代码,但我得到了以下错误

代码语言:javascript
复制
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

这会不会是我的代码有问题。也就是说,代码的前10或15行中有两个redirect_to,或者它在谈论其他东西?

代码语言:javascript
复制
<?php require_once("../../includes/initialize.php"); ?>

<? if(!$session->is_logged_in()){
    redirect_to("login.php"); } ?>

<?php
$logfile = SITE_ROOT.DS.'logs'.DS.'log.txt';
if($_GET['clear'] == 'true') {
 file_put_contents($logfile, '');
 //add the first log entry
 log_action('Logs Cleared', "by User ID {$session->user_id}");
 //redirect to this same page so that the URL won't 
 //have "clear=true" anymore
 redirect_to('logfile.php');
}
?>

<?php include_layout_templates('admin_header.php');?>

<a href="index.php">&laquo; Back</a><br/>

<br/>

<h2>Log File</h2>

<p><a href="logfile.php?clear=true">Clear log file</a></p>

<?php
if (file_exists($logfile) && is_readable($logfile) && 
    $handle = fopen($logfile, 'r')) {//read
    echo "<ul class=\"logentries\">";
    while(!feof($handle)) {
    $entry = fgets($handle);
    if(trim($entry) != "") {
    echo "<li>{$entry}</li>";
    }
    }
    echo "</ul>";
    fclose($handle);
    } else {
    echo "Could not read from {$logfile}.";
    }

?>



//Remember to give your form's submit tag a name="submit" attribute
if (isset($_POST['submit'])) {//Form has been submitted.

$username = trim($_POST['username']);
$password = trim($_POST['password']);

//Check database to see if username/password exist

$found_user = User::authenticate($username, $password);

if ($found_user) {
    $session->login($found_user);
    log_action('Login', "{$found_user->username} loggined in.");
    redirect_to("index.php");
} else {
    //username/password combo was not found in the database
    $message = "Username/password combination incorrect.";
} 
} else {//Form has not been submitted.
    $username = "";
    $password = "";
    }
?>
<?php include_layout_template('admin_header.php'); ?>
        <h2>Staff Login</h2>
        <?php echo output_message($message); ?>

        <form action="login.php" method="post">
            <table>
                <tr>
                    <td>Username:</td>
                    <td>
                        <input type="text" name="username" maxlength="30" value="<?php
                        echo htmlentities($username); ?>" />
                    </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td>
                        <input type="password" name="password" maxlength="30" value="<?php
                        echo htmlentities($password); ?>" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" name="submit" value="login" />
                    </td>
                </tr>
            </table>
        </form>
        <?php include_layout_template('admin_footer.php'); ?>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-10 19:06:21

你有一个无穷无尽的重定向循环。

如果您没有登录,"login.php“会重定向到"login.php”。如果您没有登录,"login.php“会重定向到"login.php”。如果您没有登录,"login.php“会重定向到"login.php”。如果您没有登录,"login.php“会重定向到"login.php”。等。

您可能应该使重定向仅在当前页面不是“login.php”时发生;即从该页面中删除该逻辑。

票数 5
EN

Stack Overflow用户

发布于 2011-03-10 19:05:55

代码语言:javascript
复制
<? if(!$session->is_logged_in()){
    redirect_to("login.php"); } ?>

我想这就是你的问题所在。您正在检查登录页面,以查看是否有人已登录。如果没有,您将重定向到您的登录页面,启动一个新的请求,然后它将再次执行检查。

  • 登录页面询问:用户是否已登录?不是的!将他们重定向到登录页面
  • 登录页面会询问,用户是否已登录?不是的!将他们重定向到登录页面
  • 登录页面会询问,用户是否已登录?不是的!将它们重定向到登录page
  • ad-infinitum

使用登录页面的用户不一定要登录,所以在使用该页面之前,请取消检查某人是否已登录。

票数 2
EN

Stack Overflow用户

发布于 2011-03-10 19:11:48

  • 如果您未登录,请检查登录页是否进行了重定向。
  • 在重定向之前请确保没有输出
  • 请确保在完成重定向后退出。在您的代码示例中,由于您的require和if check之间的空行,在调用redirect函数之前,您将以一些空格结束。如果我是你,我不会在没有必要的时候像你一样频繁地进出php。一直到你的第一个链接,我只看到php,但是你有3个<?php和1个<? (这也不是一个好主意。我会坚持只使用<?php).
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5258712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档