仅使 $_SESSION 中的一个选定变量过期(而不是整个 $_SESSION)
Expire only one selected variable inside $_SESSION (instead of the whole $_SESSION)
我正在编写一个程序,我需要使 $_SESSION
数组中的选定变量过期。
现在我知道如何使整个 $_SESSION
变量过期,但我的需要是 选择性 ,即仅使 $_SESSION['trial_period_1']
、$_SESSION['trial_period_credits']
变量过期1 天后。
我希望用户保持登录状态,只是希望试用期变量结束(会话超时为 1 个月)。
我正考虑这样做,但决定问问,因为我确定有人知道更优雅的解决方案。
$_SESSION['trial_period_credits'] = 4;
$_SESSION['trial_period_credits_expire'] = time() + 86400;
然后每次检查每个变量
if ( $_SESSION['trial_period_credits_expire'] < time() ) { ...
有更好的方法吗?我的解决方案看起来有点乱,因为它需要创建很多不必要的会话变量。
使用unset
。试试 -
if ( $_SESSION['trial_period_credits_expire'] < time() ) {
unset($_SESSION['trial_period_1']);
unset($_SESSION['trial_period_credits']);
}
确定密钥并使用unset
:
if ( $_SESSION['trial_period_credits_expire'] < time() ) {
unset($_SESSION['trial_period_1']);// will unset the $_SESSION for key trial_period_1 only.rest of the $_SESSION will remain.
}
我正在编写一个程序,我需要使 $_SESSION
数组中的选定变量过期。
现在我知道如何使整个 $_SESSION
变量过期,但我的需要是 选择性 ,即仅使 $_SESSION['trial_period_1']
、$_SESSION['trial_period_credits']
变量过期1 天后。
我希望用户保持登录状态,只是希望试用期变量结束(会话超时为 1 个月)。
我正考虑这样做,但决定问问,因为我确定有人知道更优雅的解决方案。
$_SESSION['trial_period_credits'] = 4;
$_SESSION['trial_period_credits_expire'] = time() + 86400;
然后每次检查每个变量
if ( $_SESSION['trial_period_credits_expire'] < time() ) { ...
有更好的方法吗?我的解决方案看起来有点乱,因为它需要创建很多不必要的会话变量。
使用unset
。试试 -
if ( $_SESSION['trial_period_credits_expire'] < time() ) {
unset($_SESSION['trial_period_1']);
unset($_SESSION['trial_period_credits']);
}
确定密钥并使用unset
:
if ( $_SESSION['trial_period_credits_expire'] < time() ) {
unset($_SESSION['trial_period_1']);// will unset the $_SESSION for key trial_period_1 only.rest of the $_SESSION will remain.
}