Header() 没有重定向 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12817846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:17:31  来源:igfitidea点击:

Header() is not redirecting PHP

phpredirecthttp-headers

提问by Sandeep Kamble

I am using following PHP code to check session. But if the session is not logged into then page is not redirect just show the black page.

我正在使用以下 PHP 代码来检查会话。但是,如果会话未登录,则页面不会重定向,只会显示黑页。

My If condition is right ! it is hitting header function .. Here is my code:

我的条件是对的!它正在击中标题功能..这是我的代码:

if(
    !isset($_SESSION['SESS_MEMBER_ID']) 
    || (trim($_SESSION['SESS_MEMBER_ID']) == '') 
    AND !isset($fb_login)
) {
    header("location:login.php?msg=Please+Login+Again.");
}

Suggestions welcome!

欢迎提出建议!

HTTP/1.1 200 OK
Date: Wed, 10 Oct 2012 10:57:14 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

回答by EM-Creations

If you're using header("Location: ");after you've output content make sure you've put ob_start();earlier in the script. ob_start();buffers your script, so nothing is output until you either call ob_end();or the end of the file is reached. Calling header("Location: ");after content has already been output to the browser can cause problems.

如果您在header("Location: ");输出内容后使用,请确保您已将其放入ob_start();脚本中。ob_start();缓冲您的脚本,因此在您调用ob_end();或到达文件末尾之前不会输出任何内容。header("Location: ");在内容已经输出到浏览器之后调用可能会导致问题。

回答by senthilbp

Put ob_start();in beginning of PHP file , it will helps,

ob_start(); 在 PHP 文件的开头,它会有所帮助,

<?php
    ob_start();
    /*
    Your code
    */
 ?>

回答by Kenny

Make sure to put an exit; or die; after the command: and make the "Location" start with uppercase

确保放一个出口;或者死; 在命令之后:并使“位置”以大写开头

if (!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '') AND !isset($fb_login)) {           
    header("Location:login.php?msg=Please+Login+Again.");
    exit;
}

回答by Terry Harvey

Make the Lcapital.

L资本。

 header("Location: login.php?msg=Please+Login+Again.");
 exit(); // just a precaution

回答by Jon

Try formatting the header correctly:

尝试正确格式化标题:

header("Location: login.php?msg=Please+Login+Again.");

And of course make sure that you are not sending any output (either on purpose or by mistake) before the call.

当然,请确保您在通话前没有发送任何输出(有意或无意)。

回答by iLaYa ツ

header("Location: login.php?msg=Please+Login+Again.");
exit();

回答by lukas.pukenis

1)Be sure that you do not send anything before the header() function

1)确保在 header() 函数之前没有发送任何内容

2)Make "location" to "Location: "

2)将“位置”改为“位置:”

回答by Lawrence Cherone

Adding to what others have said, are you sure that the cnditions are met to hit the header()

添加到其他人所说的内容,您确定符合条件以击中 header()

You could use empty() to clear up your code & Do some testing :

你可以使用 empty() 来清理你的代码并做一些测试:

//Debug
unset($_SESSION['SESS_MEMBER_ID']);
unset($fb_login);

if(empty($_SESSION['SESS_MEMBER_ID']) && !isset($fb_login)) {
    exit(header("Location: login.php?msg=Please+Login+Again."));
}

回答by marccgcom

Try changing the ANDfor &&

尝试更改AND&&

if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '') && !isset($fb_login)
    ) {
        header("location:login.php?msg=Please+Login+Again.");
    }