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: <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>';
}
?>
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?
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: <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>';
}
?>
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: <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>';
}
?>
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>";
}
?>