dongxun2089 2015-02-23 13:30
浏览 24
已采纳

在会话中存储时间

I have a code that works just fine but I have a few questions about it. I don't understand the logic of something. The code is:

<?php
session_start();

if(!isset($_SESSION['t0'])) 
{
    $_SESSION['t0']=time();
    echo $_SESSION['t0']."if<br />"; //why this is never printed?
}
else 
{

    if(time()>=($_SESSION['t0']+3))
    {
        echo $_SESSION['t0']."else-ul";
        $culoare="rgb(".rand(0,255).",".rand(0,255).",".rand(0,255).")";
        $_SESSION['t0']=time(); 
    }

}
?>

The questions would be: 1. Why the first echo is never printed? 2. Why (time()>=($_SESSION['t0']+3)) isn't always true since $_SESSION['t0'] is updated every second because of session[t0]=time() ?

Thank you!

  • 写回答

1条回答 默认 最新

  • dongyuan7110 2015-02-23 13:39
    关注
    1. First echo statement does get executed, but it happens only on very first time. Once you had your session started value for $_SESSION['t0'] is always set, so the if condition will always return false.

    2. time()>=($_SESSION['t0']+3) condition is true when 3 seconds has passed after execution of the code. So if you reload your page after 2 seconds it will not get executed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题