Display the number of visitors online.

Discussion in 'JavaScript' started by jinch, Sep 30, 2012.

  1. #1
    Hello,

    I would like to know if anyone know a javascript script to display the number of visitors online on your website ?
    No widget, just text like this : "20 people are online."

    Thanks a lot for your help.

    Best regards,
     
    jinch, Sep 30, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    The Javascript is
    displayElement.value = nrPeople + ' people are online.';
    Code (markup):
    (where displayElement is the element in which you'll display the text.)

    nrPeople is a number you get from the server, which is where you keep the number of people online. How you do that depends on the site but basically every time a session is started you increment the number and every time someone logs off you decrement it. You can use AJAX to ask the server for the number and get it back.

    (There's no Javascript code that would know the number, because Javascript runs in the user's browser, and the server, not the browser, knows how many people are still running sessions.)
     
    Rukbat, Oct 1, 2012 IP
  3. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Thanks a lot for your answer but I know nothing on AJAX. I know that it would be much more easier with PHP but I can't use PHP dode in my TPL file.
     
    jinch, Oct 1, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    You can't get the number of visitors currently on line without server-side coding, since the browser has no way of knowing how many people are connected to the server unless the server tells it.

    You have a case of "can't get there from here".
     
    Rukbat, Oct 1, 2012 IP
  5. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Yes, I know my only solutions it to figure how to insert php in my TPL file :(
     
    jinch, Oct 1, 2012 IP
  6. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    I've found a way to allow PHP in a TPL file :
    http://forums.digitalpoint.com/showthread.php?t=2565958&p=18092911#post18092911

    As for the online visitors script, here's one in PHP :

    <?php $dataFile = "visitors.txt";  $sessionTime = 30; //this is the time in **minutes** to consider someone online before removing them from our file  //Please do not edit bellow this line  error_reporting(E_ERROR | E_PARSE);  if(!file_exists($dataFile)) { 	$fp = fopen($dataFile, "w+"); 	fclose($fp); }  $ip = $_SERVER['REMOTE_ADDR']; $users = array(); $onusers = array();  //getting $fp = fopen($dataFile, "r"); flock($fp, LOCK_SH); while(!feof($fp)) { 	$users[] = rtrim(fgets($fp, 32)); } flock($fp, LOCK_UN); fclose($fp);   //cleaning $x = 0; $alreadyIn = FALSE; foreach($users as $key => $data) { 	list( , $lastvisit) = explode("|", $data); 	if(time() - $lastvisit >= $sessionTime * 60) { 		$users[$x] = ""; 	} else { 		if(strpos($data, $ip) !== FALSE) { 			$alreadyIn = TRUE; 			$users[$x] = "$ip|" . time(); //updating 		} 	} 	$x++; }  if($alreadyIn == FALSE) { 	$users[] = "$ip|" . time(); }  //writing $fp = fopen($dataFile, "w+"); flock($fp, LOCK_EX); $i = 0; foreach($users as $single) { 	if($single != "") { 		fwrite($fp, $single . "\r\n"); 		$i++; 	} } flock($fp, LOCK_UN); fclose($fp);  if($uo_keepquiet != TRUE) { 	echo '<div style="padding:5px; margin:auto; background-color:#fff"><b>' . $i . ' visitors online</b></div>'; }  ?>
    PHP:
    Don't forget to create visitors.txt with a chmod 777 :)
     
    jinch, Oct 1, 2012 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    Two changes:

    if($alreadyIn == FALSE)
    //should be
    if(!$alreadyIn)
    PHP:
    and
    if($uo_keepquiet != TRUE)
    //should be
    if(!$uo_keepquiet)
    PHP:
    The original code in both cases is called a pleonasm, and it's very inefficient (and the sign of an entry-level programmer). Boolean variables are either true or false, and will operate with "if" without comparing them to true or false. (While PHP isn't typed, it still works that way.) IOW,
    if(<some variable that's true> == TRUE)
    is the same as
    if(<some variable that's true>)

    != TRUE is always pleonastic, because it's the same as == FALSE, and - assuming we're comparing to an expression - the second way is easier to read. If we're comparing to a variable, it's "if(!variable)".

    The only valid use of the comparison would be if($variable === TRUE) IOW, if we want not only the same value, but the same type. 0 !=== FALSE, since 0 is numeric and FALSE is Boolean.
     
    Rukbat, Oct 3, 2012 IP