• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Problem in mantaining login history

hi!!
 i am trying to maintain login history of the users who have logged in to the site. i have to save login and log out time in database. i can save login time with no problem,, the problem is that i have to save logout time too. but what if user does not logout, he just closes the window or his system gets shut down due to some reason,, what should i do in that case,,, any ideas??
all ur help is more than welcome...
regards.
0
SadafRasheed
Asked:
SadafRasheed
1 Solution
 
Diablo84Commented:
Rather then logout time you might want to focus on a Last active time.

Or you could incorperate both and add an extra field to the table for last active.

You would then need a small include file (which would obviously be included within the main pages of your site) containing a database query to update this field with the current time for the user everytime they access a page.

To give an overview of what you would have, all the time the user is browsing the site this field will keep getting updated, when they stop browseing it will not be updated any longer so the database field will contain the time they was last active.

There are variations of this idea that you can implement such as a temporary table to storing rows of data (one added each time a user visits a page) which are deleted according to a time out period etc

Its basically the last active/timeout idea you want to focus on, as you know there is no great way of detecting when they close the browser etc (without the use of something like a pop up, not the best of methods).
0
 
nowaydown1Commented:
Good Day:

As the previous expert said, there really isn't any real good way to do this other than training your users to ensure they logout.  What I normally do is just develop a system that writes down when the user logs onto a page on the site.  In this way, I can record visitor information for a form of hits tracking, and know who is viewing what page when, etc.  It's a nice feature for the amount of work involved in creating such a system.  

To expound on this idea, I normally just write a function that will do an insert to a table that records a user id, a timestamp, and the name of the page they are on.  Then I call this function from each individual page.  I normally store my user ID in a session variable, so my call from each page is generally similar to something like this:

<?php

session_start();
$userID=$_SESSION['userID'];
if(empty($userID) || !isset($userID)) {
     $userID=0;     // This means they probably haven't logged in yet
}

$hTracking=new Tracking();
$hTracking->recordActivity($userID,$PHP_SELF);
?>

By using the $PHP_SELF variable, I can just copy and paste this right on the top of every page I create on the site, and not have a management nightmare on my hands.  The timestamp is generated within the recordActivity member function in this case, simply because it makes sense to do so.  

Hopefully this will bring some clarity to the idea of a very simple way to track your users through your site (and take a guess at when they most likely logged out).  

Please let me know if there is anything else that I can do to assist you.  Have a wonderful day!
0
 
arataniCommented:
Probably you could use some javascript to capture when the user has closed the window, and then call a php script to handle that.

<script>

window.onunload = function(){
  if(self.screenTop>9000)
    alert('Closed.');
  window.location.href = "logout.php";
}

</script>

I haven't tried that script out, but it should work.

Hope that helps

AJ
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now