Header() is not redirecting PHP [closed]

泪湿孤枕 提交于 2020-01-11 10:27:12

问题


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.

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

回答1:


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.




回答2:


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

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



回答3:


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;
}



回答4:


Make the L capital.

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



回答5:


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.




回答6:


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



回答7:


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

2) Make "location" to "Location: "




回答8:


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

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

//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."));
}



回答9:


Try changing the AND for &&

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


来源:https://stackoverflow.com/questions/12817846/header-is-not-redirecting-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!