Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par qasim nawaz , Seninor Web/Software Developer , CITC COMSATS Institute of Information Technolgy Lahore
Date de publication: 2015/10/08
Julfkar  Moh Umar
par 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
par 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