How to show visit time for the second time to open `welcome.php`?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to show visit time for the second time to open `welcome.php`?



There are two php file :welcome.php and check.php.

The welcome.php is as below.


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: &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.


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>";

?>



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


admin


123


password


admin :Your first visit



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


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:


127.0.0.1/welcome.php


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:


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



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


127.0.0.1/welcome.php




2 Answers
2


session_start();

if(!isset($_SESSION["visit"]))
$_SESSION["visit"] = date("Y-m-d h:i:s");
else
echo "Your first visit was $_SESSION["visit"]";



Try using Sessions



isset will check if it has a value, if not then it will set the time. However, if it is set then it will show the time.



Method1:use cookie.

Rewrite welcome.php as below.


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: &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.


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: &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.


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>";

?>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard