header()重定向后php会话丢失 [英] php session lost after header() redirect

查看:62
本文介绍了header()重定向后php会话丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试创建会话.此外,成功登录后,我使用 header() 函数重定向页面,但在重定向页面上,我不再有会话.有代码:

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:

创建会话:

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

推荐答案

您需要在两个脚本中调用 session_start(); 来启动和恢复会话.

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

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

这篇关于header()重定向后php会话丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆