简体   繁体   中英

Find the time since index.php was last visited, widget

I've done a fair amount of searching on the internet to find a solution to this issue; however, I am still unable to find a real answer.

I would like to have a widget on my site that shows the user how long in seconds it has been since someone last visited the website. For example the clock would reset to '0' if someone were the refresh the page.

Not to be be confused with the time since a specific individual last visited the site, I'm asking to find the time since ANYONE sent a request for index.php .

I assume the way to do this is tied up in a large amount of PHP, of which I am not great at. Any help, advice or pointers anyone could give would be great! Many thanks in advance.

Lastly to prevent more confusion the clock does not need to be live on the browser. It would just change each time the page is reloaded.

The best way to (as you said in your question) find the time since ANYONE sent a request for index.php is as follows:

Make a little viewer.txt file where you store the last timestamp information, and overwrite it with the function time() of the latest user visit in unix seconds, as follows. Then compare with the current time, on another file. (This method is much easier than sockets, and resembles a little database). I've programmed this code for you, put it on the page that you wish. It's completely functional, and I have tested it:

File 1 (tracking and storing latest user visit):

<?php

$file = fopen("viewer.txt", "w");

fwrite($file, "".time()."");

fclose($file);

?>

File 2 (comparing current time with last user visit):

<?php

$file = file_get_contents("viewer.txt");

$file = (int)$file;

echo "Difference in time from last visit in seconds:";

echo $file - time();

?>

Mark this answer with the checkmark on the side of it if it helped.

Feel free to ask for more of my help if you have more questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM