php session lost after header() redirect

社会主义新天地 提交于 2020-02-24 12:30:09

问题


This is first time i try to create a session. Also, after success login i redirect the page using header() function, but then on the redirected page i dont have session any more. There is the code:

creating session:

function userLogin($user){
    session_start();
    $_SESSION['username'] = $user;
    header("Location: /~klemeno/vaja10?" . SID);
    exit;
}

When browser redirect me i try to echo session like this:

if(isset($_SESSION['username'])){   
    echo $_SESSION['username'];
}
else{
    echo "No session :(";
}

回答1:


You need to call session_start(); in both scripts to start and resume the session.

See: http://php.net/manual/en/function.session-start.php




回答2:


You have to add session_start(); at the top of your PHP script(s).



来源:https://stackoverflow.com/questions/13792933/php-session-lost-after-header-redirect

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