SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if user is still there

    Hi all,

    Not sure if I posted this in the right area since its a Javascript/PHP related problem.

    I have no clue how to check if a user didn't leave the site.
    Ofcourse you can use the Onunload() or Beforeunonload() function.
    But some browsers like Opera and Safari and who know what other browsers don't support it. And I don't want to have false information about users still being there while they left.

    Is there any other alternative?
    Like expecting server side each X seconds a AJAX request from the user?
    If the user doesn't send a request, the user has left.

    Any idea how to check this server side?

    Thanks!

    -Grouver-

  2. #2
    SitePoint Member
    Join Date
    May 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am now trying to do this with sessions.
    Each time the chat updates it issues a request to a php script wich handles the chat updating. But I also want to tell the script the session needs to be reset with a new time. Since the update interval of the AJAX request is 3 seconds.
    I have set the SESSION expire on 5 seconds.

    If server side the SESSION is expired and the user doesnt send a update to reset it. Remove his IP from a text file.

    Code:
    $ip = $_SERVER['REMOTE_ADDR'];
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() -  $_SESSION['LAST_ACTIVITY'] > 15)) {
    session_destroy();   // destroy session data in storage
    session_unset();     // unset $_SESSION variable for the runtime
    if(file_exists('currentviewers.txt')){
    $current_viewers = file('currentviewers.txt');
    }
    if(count($current_viewers) != 0){
    if(in_array($ip. "\n", $current_viewers)){
    $key = array_search($ip . "\n", $current_viewers);
    unset($current_viewers[$key]);
    ksort($current_viewers);
    file_put_contents("currentviewers.txt", "");
    $fp = fopen("currentviewers.txt", w);
    foreach($current_viewers as $key => $value){
    fwrite($fp, $value);
    }
    }
    }			
    }
    $_SESSION['LAST_ACTIVITY'] = time();
    This is not working though.
    Any ideas?

  3. #3
    SitePoint Enthusiast VideoWhisper's Avatar
    Join Date
    Dec 2008
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We use some simple php code to keep track of online operators for a video messenger chat. This may be of use.

    The chat application calls a status script from time to time and this saves a file with online username:
    ...
    $dfile = fopen($dir."/$session","w");
    fputs($dfile,$username);
    fclose($dfile);
    Then based on that file datetime, keep track of online operators and return a list of operators online in that department (room):

    $onlinelatency=60;
    $onlinetime=time()-$onlinelatency;
    ...
    if (file_exists("$dir/$file/online"))
    if (is_dir("$dir/$file/online"))
    {
    $oplist="";
    $opcount=0;
    $h2=opendir("$dir/$file/online");
    while (($file2 = readdir($h2))!==false) //scan all online operators
    if (!is_dir("$dir/$file/online/$file2"))
    {
    $ftime=filemtime("$dir/$file/online/$file2");
    if ($ftime>$onlinetime)
    {
    $opcount++;
    $oplist.= ($oplist?", ":"") . implode(file("$dir/$file/online/$file2"));
    }
    else @unlink("$dir/$file/online/$file2"); //remove offline operator
    }

    if ($opcount)
    {
    $html.="<a href='$url/client.php?department=$file' target='_blank'>$imgonline <b>$file</b> is online.</a>";
    $depcount++;
    }
    else
    {
    $html.="$imgoffline <b>$file</b> is offline.";
    }

    } else;
    else $html.="$imgoffline <b>$file</b> is currently offline.";
    You can download the php live video support edition for free from our site if you want to take a look at full php code.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •