有两个php文件:welcome.php
和check.php
。
welcome.php
如下。
<?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: <input type="text" name="xm" size="12"><br />
password: <input type="password" name="Pwd" size="12"><br />
<input type="submit" value="login"><br />
</form></body></html>';
}
?>
check.php
如下。
<?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>";
}
?>
何时在用户中输入admin :Your first visit
并在welcome.php中的admin
中输入123
时,check.php将显示password
。
admin :Your first visit
这是您第一次点击127.0.0.1/welcome.php
然后第二次在浏览器中单击127.0.0.1/welcome.php
,输出如下:
welcome admin
This is your 2 th visit my website
The latest visit time is
注意:时间戳不在上面的输出中。
要第三次单击浏览器中的127.0.0.1/welcome.php
,输出如下:
welcome admin
This is your 3 th visit my website
The latest visit time is 2018-08-11 12:09:36
如何第二次显示在浏览器中打开127.0.0.1/welcome.php
的访问时间?
答案 0 :(得分:0)
session_start();
if(!isset($_SESSION["visit"])) {
$_SESSION["visit"] = date("Y-m-d h:i:s");
}else{
echo "Your first visit was {$_SESSION["visit"]}";
}
尝试使用Sessions
isset将检查其是否具有值,如果没有,则将设置时间。但是,如果已设置,它将显示时间。
答案 1 :(得分:0)
方法1:使用Cookie。
如下重写welcome.php
。
<?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: <input type="text" name="xm" size="12"><br />
password: <input type="password" name="Pwd" size="12"><br />
<input type="submit" value="login"><br />
</form></body></html>';
}
?>
方法2:使用会话。
感谢K Sudbury's
的提醒。
如下重写welcome.php
。
<?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: <input type="text" name="xm" size="12"><br />
password: <input type="password" name="Pwd" size="12"><br />
<input type="submit" value="login"><br />
</form></body></html>';
}
?>
如下所示重写check.php
。
<?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>";
}
?>