Knowing real time an user is in my site

By : mariogl

I have a website, and I have to implement (with PHP and/or JavaScript) an alert message that triggers two minutes after a visitor has entered the site. I've searched, but all solutions I've found are for an unique page. I need the timer counter to start when the user enters my site, no matter through which page. And I need that counter keeps counting while the user navigates my site's pages.

One solution could be using session variables. I can make a script that looks for this variable, if it doesn't exist means that the user is entering the site. Then I set this variable with current time. The script it's in each page, and it will be reading this variable via AJAX each x seconds and I'll know when the user is in my site since two minutes.

I don't know if it's right or not (I've not implemented yet), but I'm not pretty sure if session is the best way. If the user leaves the page but has other navigator windows opened, the session doesn't expire, and if he enters the site again, the counter will not be reset.

So, two questions:

  1. Is there a better method to have more control on the real entering and exiting?
  2. If not, is my above approach right?


By : mariogl


Something like this should work.

$alert_message = false;

    $_SESSION['time_entered'] = time();

if($_SESSION['time_entered'] =

You don't need AJAX, you just need to store the time in a session variable, and then include some JavaScript on each page, here is an example:

This video can help you solving your question :)
By: admin