?
Solved

Session Question

Posted on 2003-04-01
6
Medium Priority
?
283 Views
Last Modified: 2010-08-05
I am looking for a way to keep an active list of who is logged on to my site.  When someone logs on, it would create a new record in, let say, the "current_users" table.  That is the easy part...  Problem is how would I update that table when the user browses away from my page or closes the browser.  ASP has onstart onend methods for sessions and applications, does anything exist like this in PHP.  I tried searching around, but can't find any answers....

Pseudo code is fine if anyone knows....

-styles
0
Comment
Question by:ocgstyles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8247659
easy. I do this too. It's not perfect (impossible to detect if the user closes the browser or if his just changes URL, but anyway in the latter case, you'llsee him coming later and his session will still be active, so no problem.)

some code in 5 minutes
0
 
LVL 15

Accepted Solution

by:
VGR earned 80 total points
ID: 8247752
here it is.

Note that, and it's unusual for me, this code doesn't require register_globals=On in php.ini ;-)

<?
// file is : index.php
//
//VGR13042001 ADDed buttREFR=1 ` la place de return=1 pour le lien "affiche"
// [snip]
//VGR24022003 FIXed problhme via $isindex dans GetListeStatus()
//

require_once('browsdetect.inc'); // positionne sess_isNS et "sess_win"

// snip including settings. For you, use this :
$globCloseWindows=1;

if ($globLikeActions==1) $pathinfo=$PATH_INFO;

// points d'entrie : rien (retour de formulaires) ou 'renew' ou 'byeuser'
if (isset($pathinfo)) $action=substr($pathinfo,1);

require 'session.inc.php'; // that's where people are rejected (blacklist or negative login) or get a valid session (via a positive login)

// points d'entrie : rien (retour de formulaires) ou 'byeuser'
if (($action=="byeuser")OR(isset($byeuser))) {
    LogAction($sess_pseudo,$REMOTE_ADDR,"byeuser : $sess_pseudo a refermi index (parti).",5);
    // here you could take supplementary actions : updating a DB to say "he's gone" (I don't, but I could)
    // or destroy the session (I don't, people can come back)
    // so basically I'm just logging that he/she left the site, but you can take an other course of actions here 8-)
    echo "<SCRIPT>self.close();</SCRIPT>";
    exit;
} // byeuser case (en if pour l'instant, c'est mieux)

// hereafter I reinitialize the session data that keeps track of connected users :
//VGR09012001 ADDed cleaning of Status List
unset($sess_listeusers);
unset($sess_statususers);
unset($sess_lastDTusers);
unset($sess_iconesusers);
unset($sess_vitesseusers);
unset($sess_activiteusers);
$sess_listeusers=array();
$sess_statususers=array();
$sess_lastDTusers=array();
$sess_iconesusers=array();
$sess_vitesseusers=array();
$sess_lastDT=deltaTH(date("Y-m-d H:i:s",time()),-2,0,0); // now()-2h
$sess_iconesusers=array();
$sess_vitesseusers=array();
$sess_activiteusers=array();
//Get the list of users with their status, etc
GetListeStatus2($sess_lastDT,$sess_listeusers,$sess_statususers,$sess_lastDTusers,$sess_iconesusers,$sess_vitesseusers,$sess_activiteusers,TRUE);
// et on passe ` la suite ci-dessous
echo "<HEAD>";
echo "<meta http-referer=\"$globFReferer\">";
echo "<META NAME=\"description\" content=\"$globFDescription\">";
echo "<META NAME=\"keywords\" content=\"$globMetaKeywords\">";
// [snip]
echo "<META NAME=\"creation\" content=\"15/12/2000\">";
echo "<META NAME=\"last-modified\" content=\"$globMetaDate\">";
echo "<META NAME=\"root\" content=\"$globMetaRoot\">";
echo "<TITLE>$globFTitre</TITLE>";
?>
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JavaScript-Impaired Browsers
function ByeWin() {
windowIMA=window.open("",'Anim2','toolbar=no,location=yes,directories=no,menubar=no,scrollbars=no,status=yes,resizable=1,width=260,height=290');
windowIMA.document.write("<HTML><TITLE>Fenetre ByeBye</TITLE>"
+"<SCRIPT>setTimeout('self.close();',2000);<\/SCRIPT>"
+"<BODY BGCOLOR= '#0000ff' text= 'white'><CENTER>"
+"<P><B><?echo $globFTitre;?><P>Thank you for coming <? echo $sess_pseudo;?><BR>"
+"<BR>some bye-bye text of yours<BR>"
+"<BR>your cherished webmaster</B><BR></BODY></HTML>");
windowIMA.document.close();
window2=window.open('./index.php<? echo (($globLikeActions==1)?'/byeuser':'?byeuser=1')?>','','width=50,height=50');
}
// End Hiding -->
</SCRIPT>
</HEAD>
<frameset cols="*" rows="140,*" border="0" <? if ($globCloseWindows==1) echo "onUnload=\"ByeWin()\"";?>>
 <frame name="monitor" src="monitor.php" scrolling=YES NORESIZE>
 <frameset cols="160,*" border="0">
  <frame name="client" src="client.php" scrolling=YES NORESIZE>
  <frame name="affiche" src="affiche.php?buttREFR=1" scrolling=YES>
 </frameset>
</frameset>
<NOFRAME>

Vous ne visualisez pas les cadres. Vous ne verrez que les messages.<BR>
You're not able to see frames. You'll see only the messages.<BR>
<BR>
<BR>
Cliquez <A HREF="./affiche.php?return=1">ici</A><BR>
Click <A HREF="./affiche.php?return=1">here</A><BR>

</NOFRAME>
0
 
LVL 5

Author Comment

by:ocgstyles
ID: 8248983
Taking what I think you are tying to relay, I could make a frame set, and just keep my normal content.  But since I put the unload method in the frameset, when the user leaves my site completely, then I can mark them as gone in the database.....right?

Other things I was thinking is using flash.  Since on this webpage I have an SWF displaying the date, I could also make it periodically update my status in the database as 'active'.  Then, provided there are other people on the site, their sessions and clocks would clean up the database of timed out users.

As a side-note, I once made a Flash chatting application that runs on a web page.  I have Flash periodically check to see who is online, but that uses a lot of unnecessary bandwidth, especially when there was no changes found!  Actually if this works, I can modify my Flash app too!

Am I following your recommendation?

-styles
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 15

Expert Comment

by:VGR
ID: 8249031
1) yes and yes
I even suggest a hidden frame having that code on it. Sorry, I forgot this kind of code (different from above). You can indeed keep the very same page contents, and even the URL (like "index.php" ; all you've to do is an "auto-frameset" in index.php

2)yes. Yhis kind of "periodical update" can also be done in the hidden frame, as it can have it's own refresh period... 8-)

3)you can't really save the bandwidth for "checking the changes". All you can do is optimize the processus to do so ;-)

4) I think do.

I'll try to find again the code I posted some days ago about the auto-frameset, the hidden frame bearing the ByWin() code and such.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8249068
here it is :

it's rather easy. Using this, the URL in the browser stays always the same. I just have to not forget to put '/pipo' (or anything else) in the subsequent links to 'index.php' in my pages...

file is 'index.php' :

<?
$action=strtolower(substr($PATH_INFO,1));
//echo "'$action'<BR>"; exit;
if ($action=="") {
 $action='pipo';
 echo <<<EOS
 </head>
 <frameset rows="*, 0" frameborder=0 border=0 framespacing=0 OnLoad="window.resizeTo(793,1024);">
   <frame name=main src="index.php/$action">
   <frame name=b scrolling=no noresize src="fauxblank.php">
 </frameset>
EOS;
 exit;
} // else Continue

[... real code for page starts here...]
?>

and FYI and pay my honour debt, the 'fauxblank.php' code :

<?
session_start(); // delete if you don't need to display session variable (pseudo below)
?>
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JavaScript-Impaired Browsers
function ByeWin() {
windowIMA=window.open("",'Anim2','toolbar=no,location=yes,directories=no,menubar=no,scrollbars=no,status=yes,resizable=1,width=260,height=290');
windowIMA.document.write("<HTML><TITLE>Bye-Bye</TITLE>"
+"<SCRIPT>setTimeout('self.close();',3000);<\/SCRIPT>"
+"<BODY BGCOLOR= '#000000' text= 'white'><CENTER>"
+"<P><?echo $globFTitre;?><P>Thank you for your visit <? echo $sess_pseudo;?><BR>blah-blah-blah"
+"<BR>more bla-bla-bla,"
+"<BR><BR>Thanks.<BR>"
+"<BR>webmaster"
+"<BR><BR><a href='javascript:self.close()'><font color=red>close this window</font></A><BR>"
+"</BODY></HTML>");
windowIMA.document.close();
}
// End Hiding -->
</SCRIPT>
</head>
<body onUnload="ByeWin()">
</body>
</html>


NB : just before writing the BODY above, I have this to close the pop-up window automatically after a while (3 seconds) :
+"<SCRIPT>setTimeout('self.close();',3000);<\/SCRIPT>"

Delete it if, like me in some cases, you wanna offer a choice to the user in stead of just saying "good bye, I've been happy to meet you"

That way, the "bye bye" window appears only when the frameset is discharged (really quits the site) not when the user changes of page (the URL would have changed too, thus triggering the OnUnload event)
0
 
LVL 5

Author Comment

by:ocgstyles
ID: 8249108
cheers VGR!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question