We help IT Professionals succeed at work.

Update a field when window is closed. (sessions)

devonflyers
devonflyers asked
on
250 Views
Last Modified: 2013-12-12
Hi

I have a member login system using mysql and php 4.

I have set it up to show which members are online (logged in) and which are not.

When a member logs in the status field is updated to 1 and when they log out it is changed back to 0.  This work fine providing they remember to use the logout script.

The system is based on sessions.  When the log out script is run, it sets the status to 0 and then detroys the session.

If they don't use the logout script and just close the window the status is not changed back to 0, therefore they appear as still online.

Is there anyway around this problem?

Thanks

Comment
Watch Question

_XtSystems Manager

Commented:
You could use javascript to call some external page/script...
<body  onUnload='UnLoad()'>

in the onUnload script make it call the php code you need to destroy the sessions

Author

Commented:
would I be able to just put all the code needed after the '='?

eg <body onUnload='line1
                               line2
                               line3'>

_XtSystems Manager

Commented:
You could call some javascript to redirect to PHP but not to actually run the PHP code...

eg ( not the best example )
<body onUnload="window.open('Logout.php')">

that would have php code to reset the session value.
then on success would print
echo "<script>self.close();</script>";

this would close the window.
-- or something like that
_XtSystems Manager

Commented:
to clearify what I just wrote you have two files
one the appliation... this contains
  <body onUnload="window.open('Logout.php')">

then you have a second file named "Logout.php" which contains
  <?
      if ( your code to reset the session works ){
           echo "<script>self.close();</script>";
      }
  ?>

if they don't have javascript enabled... then nothing happens... but you have to use some type of client side script or a session that has a timed experation...

Author

Commented:
As i have other pages that only the members have access to, would this be triggered when moving between them?
_XtSystems Manager

Commented:
not if it is contained in a frame...

Author

Commented:
All the pages open in the same frame, so it should be ok?

Author

Commented:
I have tried it but nothing seems to happen when the window closes. - still logged in.

application:

       <html>
       <body bgcolor="#FFFFCC" onUnload="window.open('logout2.php')">
      
       <table border="0" cellpadding="0" cellspacing="0" ......... etc


logout2.php:

<?php>
session_start();
      $email = $_SESSION['valid_user'];
            
      $status = 0;

      $dbh=mysql_connect ("localhost", "xyz", "xyz") or die ('Database unavailable.  Please try again later. ' . mysql_error());
      
      mysql_select_db ("xyz");

      $query = "update members set status = ('$status') where email='$email'";
            
      $result = mysql_query($query, $dbh);
            
      mysql_close($dbh);

      //store to test if they *were* logged in
      $old_user = $_SESSION['valid_user'];
      unset($_SESSION['valid_ser']);
      session_destroy();
               
                echo "<script>self.close();</script>";
?>
Richard DavisSenior Web Developer
CERTIFIED EXPERT

Commented:
I must say that I have been attempting to battle this very beast myself, and I tried to utilize the onUnload event, but found that as soon as you switch pages, or for clarity's sake, as soon as new page headers have to be sent (the same as loading a new php or html file), this has the effect of appearing as a closing browser and so then my member's logged out with each page transition, so, although these are all excellent suggestions, I personally have yet to find that golden key that allows me the same thing that this person is seeking to achieve also.

I have been attempting to try to create some sort of a more dedicated "state" based detection method, but if you make it dependant on cookies and the client's software firewall or adware/spyware program is intercepting cookie traffic, then cookies are pretty much a dead creature. I've attempted to use persistant $_SESSION values, but this still relies on whether the client's cookies are enabled properly (at least that's the way I have my php.ini config'd is to use cookies).

The only other sensable thing I have even been able to consider as an option would be to somehow create an idle timeout routine, but have yet to determine the best approach to this yet.
You could add a "last access" field to the user records, then to display the list of logged in users you select all those who last accessed the site within, say, the last minute.

If that's too crude (e.g. if someone stays on one page for more than a minute they'll appear to be logged out) you could write some Javascript which uses setTimeout to periodically request a special page (using AJAX type stuff) which updates the "last access" field.

Author

Commented:
Thanks for all your ideas,  I have decided just to show the date that each member was last logged in, but i have one small problem with coding.

I have my login script insert the date into a field named 'date' in my 'members' table.

When I recall this data I would like it to be displayed something like: dd mm yy and not yyyy dd mm as it is in mysql.

I understand that I have to use the DATE_FORMAT(date, '%d %m %y') function but not sure how to include it within my display members script.

I currently have the following which works fine:

$dbh=mysql_connect ("localhost", "xyz", "xyz") or die ('Database unavailable.  Please try again later. ' . mysql_error());
      
            mysql_select_db ("my_database");

            $query = 'select fname, surname, telephone, email from members '."ORDER BY `surname` ASC ";

            $result = mysql_query($query, $dbh);

            $num_rows = mysql_num_rows($result) or die ("Error");

            $i=0;
            while ($i < $num_rows)
            {
            $fname = mysql_result($result,$i,"fname");
            $surname = mysql_result($result,$i,"surname");
            $telephone = mysql_result($result,$i,"telephone");
            $email = mysql_result($result,$i,"email");


                                $i=0;
            while ($i < $num_rows)
            {
            $fname = mysql_result($result,$i,"fname");
            $surname = mysql_result($result,$i,"surname");
            $telephone = mysql_result($result,$i,"telephone");
            $email = mysql_result($result,$i,"email");
echo '<tr>';
                                echo '<td><center><font face="Tahoma" size="2" color="#000000">'.$fname.'</font></td>';
                                echo '<td><center><font face="Tahoma" size="2" color="#000000">'.$surname.'</font></td>';
            echo '<td><center><font face="Tahoma" size="2" color="#000000">'.$telephone.'</font></td>';
            echo '<td><center><font face="Tahoma" size="2" color="#000000"><a href="mailto:'.$email.'">'.$email.'</a></font></td>';        

echo '</tr>';

$i++;
                     
 }

Could you please tell me where I need to include the DATE_FORMAT() as I have tried many ways and can't figure it out.

Thanks
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
That it! Thanks for you help
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.