Redirect not working with Header(Location ) and session variable

不问归期 提交于 2019-12-06 10:41:41

In your register.php, you can't test for the session variable before you issue the session_start, so your code should be more like:

session_start(); 
 if (isset($_SESSION['color'])) {
            header('Location: http://mydomain.com/thankyou.php');
    }
 else {
 // Something else....

EDIT:

Another thing I've found useful when trying to set session variable in conjunction with redirects is to proceed to the redirect only after running a function. Here's how it would work:

$throwAwayVariable = setColor('blue');
if($throwAwayVariable ){  // separated out into a function so it wouldn't redirect before the session variable was saved
    session_write_close();
    header("Location: http://mydomain.com/thankyou.php");
}

function setColor($color){
    @session_start();
    $_SESSION['color']='blue';
    return true;
}

Since not all your code is posted, you'll have to figure out where this goes, but I've always had my session vars work after this process.

Make sure you use exit(0); right after you do a header redirect otherwise php will still parse and run the rest of your script, sometimes it can cause some funny behaviour.

Your session_start() call in register.php needs to be BEFORE you call any $_SESSION variables.

I have the same issue, then I try to add session_start and session_write_close, and it works!

session_start();
$_SESSION['status'] = 'Updated Poem successfully';
session_write_close(); 
header("location: index.php");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!