0

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.

jasonlam604
  • 1,456
  • 2
  • 16
  • 25
ThankYOU
  • 317
  • 2
  • 8
  • I'd have a 2 column table, page (varchar), lastvisit (datetime) and update the lastvisit column on each load. You'd have to manually create the page records. – chris85 Feb 15 '15 at 23:19

1 Answers1

0

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.

seanlevan
  • 1,367
  • 3
  • 15
  • 33
  • I preferred text files over databases a while ago but DBs have quite a few advantages over text files. http://stackoverflow.com/questions/2356851/database-vs-flat-files I don't think the bast way to handle any data is with text files. For example think about 10 or 100 users visiting the OP's page at once with this solution. – chris85 Feb 15 '15 at 23:28
  • For something that isn't gigantic scale and will only store a few bytes of data, I don't see what the issue is. Though I agree with your point overall. @chris85 – seanlevan Feb 15 '15 at 23:34
  • Many thanks, the project is currently on a very small scale, however I shall investigate into using databases for a solution aswell. Many thanks! – ThankYOU Feb 16 '15 at 00:19
  • Could you mark my answer with a checkmark? @ThankYOU I did put effort into it. – seanlevan Feb 16 '15 at 00:22
  • Hi sorry, yes I shall. Just a quick question, this is at the top of my index.php. ` . . . etc` Then later on I have put `` I have also got a viewer.txt in the same directory, however the reading is only showing 0, even after being left for a few minutes. Thank you very much! – ThankYOU Feb 16 '15 at 00:33
  • Sure, no problem. And no, you should put the first script at the end of the page, and the last one first. :) @ThankYOU It's the other way around, so that the file gets written AFTER the previous file has been outputted. – seanlevan Feb 16 '15 at 00:58