简体   繁体   中英

php session lost after header() redirect

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 :(";
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM