douou9094747 2018-08-11 07:26
浏览 142
已采纳

如何显示第二次打开`welcome.php`的访问时间?

There are two php file :welcome.php and check.php.
The welcome.php is as below.

<?php
session_start();
if($_COOKIE["user"]["xm"] <> "")
{
    $visnum = intval($_COOKIE["user"]["num"])+1;
    setcookie("user[num]" , $visnum , time()+3600);
    setcookie("user[dt]" , date("Y-m-d h:i:s") ,time()+3600);
    echo "welcome  " . $_COOKIE["user"]["xm"];
    echo  "<br />This is your " . $visnum . "  th" . "  visit my website";
    echo  "<br />The latest visit time is  " .$_COOKIE["user"]["dt"] ;

}
else
{
    echo
        '<html><body><form method="post"  action="check.php">
           user: &nbsp;&nbsp;<input type="text" name="xm" size="12"><br />                        
           password:&nbsp;&nbsp;<input type="password" name="Pwd" size="12"><br />
          <input type="submit" value="login"><br />
        </form></body></html>';
}
?>

The check.php is as below.

<?php
session_start();
if($_POST["xm"] == "admin" && $_POST["Pwd"] == "123")
{
    setcookie("user[xm]" , $_POST["xm"] , time()+3600);
    setcookie("user[dt]" , date("Y-m-d h:i:s") , time()+3600);
    echo  $_POST["xm"] . "  :Your first visit  ";
}
else
{
    echo  "<script>alert('user name or password is wrong');location.href = 'welcome.php';</script>";
}
?>

The check.php will show admin :Your first visit when to input admin in user and 123 in password in welcome.php.

  admin :Your first visit 

This is your first time to click 127.0.0.1/welcome.php

Then to click 127.0.0.1/welcome.php in browser for the second time,the output is as below:

welcome admin
This is your 2 th visit my website
The latest visit time is

Notice:time stamp is not in the above output.
To click 127.0.0.1/welcome.php in browser for the third time,the output is as below:

welcome admin
This is your 3 th visit my website
The latest visit time is 2018-08-11 12:09:36

How to show visit time for the second time to open 127.0.0.1/welcome.php in my browser?

  • 写回答

2条回答 默认 最新

  • dou448172583 2018-08-11 12:03
    关注

    Method1:use cookie.
    Rewrite welcome.php as below.

    <?php
    session_start();
    if($_COOKIE["user"]["xm"] <> "")
    {
        $visnum = intval($_COOKIE["user"]["num"])+1;
        setcookie("user[num]" , $visnum , time()+3600);
        echo "welcome  " . $_COOKIE["user"]["xm"];
        echo  "<br />This is your " . $visnum . "  th" . "  visit my website";
        echo  "<br />The latest visit time is  " .$_COOKIE["user"]["dt"] ;
        setcookie("user[dt]" , date("Y-m-d h:i:s") ,time()+3600);
    }
    else
    {
        echo
            '<html><body><form method="post"  action="check.php">
               user: &nbsp;&nbsp;<input type="text" name="xm" size="12"><br />                        
               password:&nbsp;&nbsp;<input type="password" name="Pwd" size="12"><br />
              <input type="submit" value="login"><br />
            </form></body></html>';
    }
    ?>
    

    Method2: use session.
    Thanks for K Sudbury's reminder.
    Rewrite welcome.php as below.

    <?php
    session_start();
    if($_COOKIE["user"]["xm"] <> "")
    {
        $visnum = intval($_COOKIE["user"]["num"])+1;
        $expire = intval($_COOKIE["user"]["expire"]);
        setcookie("user[num]" , $visnum , time()+3600*$expire);
        echo "welcome  " . $_COOKIE["user"]["xm"];
        echo  "<br />This is your " . $visnum . "  th" . "  visit my website";
        echo  "<br />The latest visit time is  " .$_SESSION["visit"] ;
        $_SESSION["visit"] = date("Y-m-d h:i:s");
    }
    else
    {
        echo
            '<html><body><form method="post"  action="check.php">
               user: &nbsp;&nbsp;<input type="text" name="xm" size="12"><br />                        
               password:&nbsp;&nbsp;<input type="password" name="Pwd" size="12"><br />
              <input type="submit" value="login"><br />
            </form></body></html>';
    }
    ?>
    

    Rewrite check.php as below.

    <?php
    session_start();
    if($_POST["xm"] == "admin" && $_POST["Pwd"] == "123")
    {
        setcookie("user[xm]" , $_POST["xm"] , time()+3600);
        setcookie("user[expire]" ,1 , time()+3600);
        setcookie("user[dt]" , date("Y-m-d h:i:s") , time()+3600*$expire);
        $_SESSION["visit"] = date("Y-m-d h:i:s");
        echo  $_POST["xm"] . "  :Your first visit  ";
    }
    else
    {
        echo  "<script>alert('user name or password is wrong');location.href = 'welcome.php';</script>";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100