嗨大家我在php wordpress网站上声明了一个变量会话,但是在没有导航的5分钟后它丢失了值,但是会话仍然有效,并且php.ini中的session.gc_maxlifetime设置为默认值1440秒( 24分钟)。这是我的代码:
public function __construct() {
echo '<script>console.log("0: '.session_status().'")</script>';
if ( ( $_SESSION['logged'] != null ) && (isset($_SESSION['logged'])) )
{
self::$profile = $_SESSION['logged'];
self::$favoriteTeam = self::$profile->favorite_team;
echo '<script>console.log("1: '.self::$profile->fb_user_id.'")</script>';
echo '<script>console.log("2: '.self::$profile->favorite_team.'")</script>';
echo '<script>console.log("3: '.self::$favoriteTeam.'")</script>';
}
}
public function(){ $_SESSION['logged'] = self::$profile = $user; }
更新:我忘了提到这是一个wordpress项目,是的,如果我尝试调试,它会保存到会话变量中的内容:
echo '<script>console.log("1: '. $_SESSION['logged'].'")</script>';
答案 0 :(得分:1)
您是否在使用该值之前先启动会话
public function __construct() {
session_start();
echo '<script>console.log("0: '.session_status().'")</script>';
if ( ( $_SESSION['logged'] != null ) && (isset($_SESSION['logged'])) )
{
self::$profile = $_SESSION['logged'];
self::$favoriteTeam = self::$profile->favorite_team;
echo '<script>console.log("1: '.self::$profile->fb_user_id.'")</script>';
echo '<script>console.log("2: '.self::$profile->favorite_team.'")</script>';
echo '<script>console.log("3: '.self::$favoriteTeam.'")</script>';
}
}
public function()
{
$_SESSION['logged'] = self::$profile = $user;
}
答案 1 :(得分:1)
这可能是因为cookie生命周期设置为低。查看this link
另请不要忘记session_start()
。
请勿尝试在()
中强制设置为$ user:
if (isset($user) || !isset(self::$profile)) {
$_SESSION['logged'] = self::$profile = $user;
}