Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
php script to show total users online

 
Post new topic   Reply to topic    dna88 Forum Index -> Web scripting language Discussion Forum
Author Message
emm
Power User
Power User


Joined: 13 Jul 2004
Posts: 310

Post Post subject: php script to show total users online Reply with quote

This is not a counter script I am looking for. But is there a way to show the total number of visitors online in my website with php? Not just how many people visiting the current page, but everyone online. Something like this forum that shows the number of users online. I want it in php and it would be best if I did not have to use any database.
_________________
“You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Sun Nov 07, 04 6:36 am
Back to top
emm View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

You can use something like this:

Code:
<?

$ip = $REMOTE_ADDR;
$time = time();
$minutes = 15;
$found = 0;
$users = 0;
$user  = "";

$tmpdata = $DOCUMENT_ROOT."/online/data";

if (!is_file("$tmpdata/online.txt"))   
   {
   $s = fopen("$tmpdata/online.txt","w");
   fclose($s);
   chmod("$tmpdata/online.txt",0666);
   }

$f = fopen("$tmpdata/online.txt","r+");
flock($f,2);

while (!feof($f))
   {
   $user[] = chop(fgets($f,65536));
   }

fseek($f,0,SEEK_SET);
ftruncate($f,0);

foreach ($user as $line)
   {
   list($savedip,$savedtime) = split("\|",$line);
   if ($savedip == $ip) {$savedtime = $time;$found = 1;}
   if ($time < $savedtime + ($minutes * 60))
      {
      fputs($f,"$savedip|$savedtime\n");
      $users = $users + 1;
      }
   }

if ($found == 0)
   {
   fputs($f,"$ip|$time\n");
   $users = $users + 1;
   }

fclose ($f);
print "Visitors  Online :&nbsp;<font color=black>$users</font>
";
?>


Save the code as online.php and create a folder called data and make a file named data.txt in it with the text 172.16.0.2|1038236463 . Make a folder on your server like online and put all of the files in it. The file in the data folder online.txt has to have permission of 666. After this is all done where ever you want to have the number of visitors on line appear put in a php include like <? include"/online/online.php";?>.

Let me know if it works for you.
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Mon Nov 08, 04 12:40 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
emm
Power User
Power User


Joined: 13 Jul 2004
Posts: 310

Post Post subject: Reply with quote

I have not tried the script yet. I shall let you know after trying the script.
_________________
“You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Tue Nov 09, 04 5:59 am
Back to top
emm View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Web scripting language Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group