Results 1 to 4 of 4

Thread: # of people Online -- Please help

  1. #1
    Join Date
    Mar 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default # of people Online -- Please help

    Is there any code for telling the numbers of people currently online in my site and also an counter...

    for example:
    Like 18 people are currently online
    1000 total vistors

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I would recommend using server-side scripting (PHP) for this. Below is a counter script that uses AJAX to increment the counter in "real-time". (Using PHP).

    counter.php (configure the parts in red to point to the text files that will be used for the counter - make sure these files have writable permissions [777]):

    Code:
    <?php
    $timeout = 5;
    
    session_start();
    header("Cache-Control: no-cache, must-revalidate");
    
    //configuration
    
    $counterTxt = "counter.txt"; //text file for the currently online counter
    $hitsTxt = "hits.txt"; //text file for the total hits counter.
    
    //###################### CALCULATE USERS ONLINE #########################
    
    $userip = $_SERVER['REMOTE_ADDR'];
    $time = time();
    $usersonline = 0;
    $alreadyonline = 0;
    
    
    
    $onlinetxt = fopen("$counterTxt","r+");
    flock($onlinetxt,2);
    while (!feof($onlinetxt)) 
    $user[] = chop(fgets($onlinetxt,65536));
    
    fseek($onlinetxt,0,SEEK_SET);
    ftruncate($onlinetxt,0);
    foreach ($user as $line) {
      @list($savedip,$savedtime) = split("\|",$line);
      if ($savedip == $userip) {
        $savedtime = $time;
        $alreadyonline = 1;
      }
      if ($time < $savedtime + ($timeout)) {
        fputs($onlinetxt,"$savedip|$savedtime\n");
        $usersonline = $usersonline + 1;
      }
    }
    if (!$alreadyonline) {
      fputs($onlinetxt,"$userip|$time\n");
      $usersonline = $usersonline + 1;
    }
    fclose ($onlinetxt);
    
    //###################### CALCULATE HIT COUNTER #########################
    
    $stats = file("$hitsTxt");
    $stats = explode("|",$stats[0]);
    $unique_today = $stats[0]+0;
    $unique_total = $stats[1]+0;
    $old_today = $stats[2];
    	
    $today = date("Ymd");
    if($old_today != $today)
      $unique_today = 0;
    
    if(!isset($_SESSION['counted']) || ($_SESSION['counted'] != 1)){
      $unique_today++;
      $unique_total++;
      $_SESSION['counted']=1;
    }
    
    $fp = fopen("$hitsTxt","w");
    fwrite($fp,$unique_today."|".$unique_total."|".$today);
    fclose($fp);
    
    //######################## DISPLAY STATISTICS ##########################
    
    echo 'Currently Online: '.$usersonline.'<BR>
            Hits Today: '.$unique_today.'<BR>'
            Total Hits: '.$unique_total;
    ?>
    Then place this code wherever you want the counter to appear.

    Code:
    <div id="counter"></div>
    
    <script type="text/javascript" language="javascript">
    if (window.XMLHttpRequest) { 
        xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    function callServer(url) {
      xmlHttp.open("GET", url, true);
      xmlHttp.onreadystatechange = updatePage;
      xmlHttp.send(null);
    }
    function updatePage() {
      if (xmlHttp.readyState == 4) {
        var response = xmlHttp.responseText;
        document.getElementById("counter").innerHTML = response;
        setTimeout("callServer('counter.php')",2500);
      }
    }
    callServer("counter.php");
    </script>
    Hope this helps, and let me know if you need any more help.
    Last edited by thetestingsite; 03-09-2007 at 09:26 PM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I would recommend using server-side scripting (PHP) for this.
    Recommend PHP, perhaps, but this REQUIRES server side scripting. Javascript just has access to the individual user, with no way of communicating across the server to other users, so server side scripting is the way to connect the users, such as with a counter, etc.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Sorry, forgot to put in the description about the AJAX contacting the PHP script. I guess I thought it was clear that the above script uses PHP, but I guess not. Anyways, editted (spelling?) my last post.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

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
  •