2

如果用户在不相关的页面上设置下拉菜单的值来设置它,我有这段代码会将用户重新定位到 index.php,请检查我的代码。

if(isset($_GET['d'])&&empty($_GET['d'])===false){
        $cur_page=$_SERVER['PHP_SELF'];
        $current_page = substr($cur_page,1);
        $possible_page = array('terms.php','contact.php','about.php');

        if(in_array($current_page,$possible_page)){
               header('Location:/index.php?d='.$_GET['d'].'');
       exit();
    }else{
       echo $_GET['d'];
    }

它在我的本地服务器上工作正常,但在实时服务器上却不行?

4

2 回答 2

2

在 php 脚本的最开始添加ob_start();。如果它包含另一个文件,那么最后不要使用?>。谢谢

于 2013-04-08T16:11:31.783 回答
0

我总是使用这个小方法,并且在所有情况下都能完美运行。

public static function Redirect($sec, $file)
{
    if (!headers_sent())
    {
        header( "refresh: $sec;url=$file" ); 
    }
    elseif (headers_sent())
    {
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="'.$sec.';url='.$file.'" />';
        echo '</noscript>';
    }
    else
    {
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$file.'";';
        echo '</script>';
    }
}

在最后一种情况下,它肯定会重定向。

于 2013-07-11T05:38:34.193 回答