Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to track user logged out or not in php? when user is idle?

user-image
Question added by qasim nawaz , Seninor Web/Software Developer , CITC COMSATS Institute of Information Technolgy Lahore
Date Posted: 2015/10/08
Julfkar  Moh Umar
by Julfkar Moh Umar , Sr. Software Engineer , Aakash Edutech Private Limited

There are a few ways to do this. Here are a couple...

  • set a session expiry time, such that after a certain amount of time, the session expires and is no longer valid.

  • set a time flag as session data, and check if their session is still  new enough to keep them logged in each pageload.

Mostafa Mohammed Naguib
by Mostafa Mohammed Naguib , Senior back-end developer , Egpay payment solution

since Php is server side, there are no way to check it unless a page reload or an ajax calland you can always save a time stamp in $_sessions or $_cookie for checking later on, and have a call in jQuery like so

 

 function CHeckLogedin(){

         $.get('checklogin.php', {}, function(returndata){

             if(returndata == 'true'){

                //user still logged in

                alert('you are still logged in');

              } else {

               //user logged out

               window.location='signin.php';

         });

}

next in checklogin.php page

do something like

<?

start_session();

$next5min = strtotime('+5 minute');

if ($_SESSION[time] < $next5min){

        echo 'true';

} else {

        echo 'false';

}

?>

and whenever user moves between pages change his $_SESSION[time] to time(), so its always updatedand in whichever page you wanna have that check do something like

setTimeout(CHeckLogedin(),);

i haven't tested this code, so it might have some bugs

 

Hope that helped,have a good day.

More Questions Like This