我无法解决“重定向循环”

时间:2014-08-31 04:11:40

标签: php redirect

此网页有重定向循环。 以上内容已在此处进行了广泛讨论和回答,但在遵循此处的建议后,仍无法解决"标题("位置:...)"

的问题。

当用户点击忘记密码时执行代码"链接电子邮件。该链接附加了随机字符串,如" ... /?trs = randomstringstoredindatabase"。

代码执行时:

如果字符串有效,我会得到以下结果:

Case with code lines "header...;: & "exit;" uncommented:
    URL in browser: http://localhost/pl_00_00/pl_process_forgot_password_back.php/pl_reset_password.php/index.php
    Chrome displays: This webpage has a redirect loop

Case with code lines "header...;: & "exit;" commented out:
    URL in browser: http://localhost/pl_00_00/pl_process_forgot_password_back.php/?trs=015df6fcf5bdcd4d9a339d5ca79d27a7 - correct
    Chrome displays echo: "Redirect to: pl_reset_password.php/?memid=11" - as expected

否则(字符串无效):

Case with code lines "header...;: & "exit;" uncommented:
    URL in browser: http://localhost/pl_00_00/pl_process_forgot_password_back.php/index.php
    Chrome displays: This webpage has a redirect loop

Case with code lines "header...;: & "exit;" commented out:
    URL in browser: http://localhost/pl_00_00/pl_process_forgot_password_back.php/?trs=015df6fcf5bdcd4d9a339d5ca79d27a
    Chrome displays echo: "Redirect to: index.php" - as expected

所以,当不使用" header()"功能逻辑似乎是正确的。一旦我取消注释" header()" &安培; "出口"并注释掉echos和var_dumps,问题出现并且没有发生重定向。这是我使用的代码(PASSWORD_RESET_PAGE& HOME_PAGE是其他地方定义的常量,两者都没有函数"标题("位置...")"这可能导致重定向循环它们):

<?php 
# pl_process_forgot_password_back.php
require_once ("lib/required.php");

    # $_GET the string from url check and if matches, active and not expired display password change form
    $trs_from_email = $_GET['trs'];
    # retrieve user data    
    $query = "SELECT * FROM tbl_member WHERE temporary_random_string    = '" . $trs_from_email . "'";

        $data_retrieved = sql_get_results_array($query); # retrieve data
        if (db_affected_rows() == 1) { # string matched - 1 row selected
            if ($data_retrieved[0]['random_string_active']){ # string active
                if ($data_retrieved[0]['random_string_expiry'] > time()){ # string not expired
                    # all ok - display reset password form                      
                    header ( "Location: " . PASSWORD_RESET_PAGE . '/?memid=' . $data_retrieved['id'] ); # send them to page with reset-password form
                    exit;   

echo '<br>' . '# all ok - display reset password form';
echo '<br>' . 'Redirect to: ' . PASSWORD_RESET_PAGE . '/?memid=' . $data_retrieved[0]['id'];
var_dump($_SESSION);
var_dump($data_retrieved);
die;                        
                } else { # string expired
                    $_SESSION['popup_msg_id'] = 17; # this is to popup request expiry message   
                } # /if string not expired
            } else { # string inactive
                $_SESSION['popup_msg_id'] = 18; # request already processed message 
            } # /if string active
        } else { # string not matched
            $_SESSION['popup_msg_id'] = 19; # string not matched message    
        } # /if valid string found

    header ( "Location: " . HOME_PAGE ); # send them to homepage and display popup error
    exit;

echo '<br>' . '# some error';
echo '<br>' . 'Redirect to: ' . HOME_PAGE;
var_dump($_SESSION);
var_dump($data_retrieved);                      
die;                        
?>

任何人都可以看到为什么上面的代码导致&#34;重定向循环&#34;?

1 个答案:

答案 0 :(得分:0)

请注意,dieexit是等效的,因此选择一个。 在脚本中点击dieexit后,脚本将不会继续执行。因此你要切断你的echo语句和var_dumps,如果你想看到它们,它应该在header之前,但重定向可能非常快,这意味着它们可能根本看不到。

如果您要重定向到当前所在的同一页面,或者如果您继续在后续页面上重定向用户,则可能会发生重定向循环。如果您确定不是这种情况,那么您需要删除与您的网站相关联的所有Cookie和浏览器缓存,以解决问题。

以下示例并按正确顺序放置您的代码。

<?php 
# pl_process_forgot_password_back.php
require_once ("lib/required.php");

    # $_GET the string from url check and if matches, active and not expired display password change form
    $trs_from_email = $_GET['trs'];
    # retrieve user data    
    $query = "SELECT * FROM tbl_member WHERE temporary_random_string    = '" . $trs_from_email . "'";

        $data_retrieved = sql_get_results_array($query); # retrieve data
        if (db_affected_rows() == 1) { # string matched - 1 row selected
            if ($data_retrieved[0]['random_string_active']){ # string active
                if ($data_retrieved[0]['random_string_expiry'] > time()){ # string not expired
                    # all ok - display reset password form                      

                    echo '<br>' . '# all ok - display reset password form';
                    echo '<br>' . 'Redirect to: ' . PASSWORD_RESET_PAGE . '/?memid=' . $data_retrieved[0]['id'];
                    var_dump($_SESSION);
                    var_dump($data_retrieved);

                    header ( "Location: " . PASSWORD_RESET_PAGE . '/?memid=' . $data_retrieved['id'] ); # send them to page with reset-password form
                    exit; 
                } else { # string expired
                    $_SESSION['popup_msg_id'] = 17; # this is to popup request expiry message   
                } # /if string not expired
            } else { # string inactive
                $_SESSION['popup_msg_id'] = 18; # request already processed message 
            } # /if string active
        } else { # string not matched
            $_SESSION['popup_msg_id'] = 19; # string not matched message    
        } # /if valid string found

    echo '<br>' . '# some error';
    echo '<br>' . 'Redirect to: ' . HOME_PAGE;
    var_dump($_SESSION);
    var_dump($data_retrieved);                        

    header ( "Location: " . HOME_PAGE ); # send them to homepage and display popup error
    exit;                    
?>