yeah, the easiest way is to use pre-made scripts or services offering that. Im using whos[dot]amung[dot]us in my blog and are very glad with that.
This is a discussion on how many users online, PHP how many users online, PHP within the Programming forums, part of the Web Designing & Development category; is there a way to display how many users are online in the site in PHP without utilizing mysql?...
is there a way to display how many users are online in the site in PHP without utilizing mysql?
yeah, the easiest way is to use pre-made scripts or services offering that. Im using whos[dot]amung[dot]us in my blog and are very glad with that.
create a blank file and name it [COLOR=Red]online.txt
[/COLOR]
create a file[COLOR=Red] online.php [/COLOR]and add the below code to it.
Upload to your server and give [COLOR=Blue]online.txt [/COLOR]write permissions.
PHP Code:<?php
$userip = $_SERVER['REMOTE_ADDR'];
$time = time();
$usersonline = 0;
$alreadyonline = 0;
$onlinetxt = fopen("online.txt","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 + ($session_length)) {
fputs($onlinetxt,"$savedip|$savedtime\n");
$usersonline = $usersonline + 1;
}
}
if (!$alreadyonline) {
fputs($onlinetxt,"$userip|$time\n");
$usersonline = $usersonline + 1;
}
fclose ($onlinetxt);
$num= array("0", "0");
shuffle($num);
$q=$num[0];
$usersonline = $usersonline + $q;
if($usersonline==1){
echo "<center><b><FONT color=\"#FF00FF\">1 useronline</FONT></b></center>";
} else {
echo "<center><b><FONT color=\"#FF00FF\">".str_replace("%1",$usersonline,"%1 usersonline")."</FONT><b></center>";
}
?>
Why not just create a session on page load, and assign $_SESSION["user"] to "Guest" or another anonymous name. Then to display Who's Online just count the session array. Haven't tried this method first hand (never had a reason to show this type of data) but I don't see why it wouldn't work.
All in all it would be about 3 lines of code.
Hello rosahuset
I tried your code, but when i refresh page it shows 0 useronline
the easiest way to count online users is to create a new $_SESSION for each user and then count the number of files in your "session.save_path" directory.
Hi, rosahuset, the code is not working properly.
OOH i got useful information..
it will be very Valuable information . thnx
:cool:الجياش:cool:
the easiest method to rely web users is to make a new $_SESSION for each individual and then rely the variety of details in your "session.save_path" catalog.
Bookmarks