Log In · Register

 

Help Topic Rules and Requirements

For a list of all requirements and guidelines pertaining to posting a new Help topic, please click here.

This Month's Contests | Staff Member of the Month | Hosts Looking for Hostees | Hostees looking for Hosts | BigBookofResources

Submission Guidelines

 
Reply to this topicStart new topic
PHP Online Users
Mikeplyts
post Aug 15 2010, 05:41 PM
Post #1


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



So, it took me a while, but I think I managed to come close to a PHP script that displays how many visitors are online at once. However, I have a bit of a problem. It doesn't necessarily work. Let me show you the function first.

This is my little function, online():
CODE
function online() {
$root = realpath($_SERVER['DOCUMENT_ROOT']);
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$users = array();
$online = array();
$file = $root . '/includes/template/online.txt';
$session = 25;

if (!file_exists($file)) {
$handle = fopen($file, 'w+');
fclose($handle);
}

$handle = fopen($file, 'r');
flock($handle, LOCK_SH);
while (!feof($file)) {
$users[] = rtrim(fgets($handle, 32));
}
flock($handle, LOCK_UN);
fclose($handle);

$x = 0;
$y = false;
foreach ($users as $key => $data) {
list( , $z) = explode('|', $data);
if ($time - $z >= $session * 60) {
$users[$x] = '';
}
else {
if (strpos($data, $ip) !== false) {
$y = true;
$users[$x] = $ip . '|' . $time;
}
}
$x++;
}

if ($y == false) {
$users[] = $ip . '|' . $time;
}

$handle = fopen($file, 'w+');
flock($handle, LOCK_EX);
$i = 0;
foreach ($users as $single) {
if ($single != '') {
fwrite($handle, $single . '\r\n');
$i++;
}
}
flock($handle, LOCK_UN);
fclose($handle);

if ($a != true) {
echo $i . 'online';
}
}

Everything looks great but I get a little warning.
CODE
Warning: feof(): supplied argument is not a valid stream resource


I'm thinking it's the path ($file) but I don't know what to put there. Any thoughts?
 
mipadi
post Aug 15 2010, 11:44 PM
Post #2


Senior Member
******

Group: Administrator
Posts: 2,647
Joined: Apr 2008
Member No: 639,265



Pass the file handle ($handle) to feof().
 
Mikeplyts
post Aug 16 2010, 06:13 AM
Post #3


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



*sigh* I hate not noticing such simple things.

Anyway, yeah. Thanks.
 

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: