Click to See Complete Forum and Search --> : Php online users


marshy28
04-02-2003, 01:05 PM
Can anybody help me and say why i cant get this simple piece of php to work.

I only want to have (users online:3) to work.
This is the line within my index.php:

<? include"/online/online.php";?>

and this is the online.php page of 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 "<strong><font face=Comic Sans Ms color=red>Online Visitors:&nbsp;$users</font></strong>";
?>

Why does it come up with the error and yes i have chmod the files online.php and the file data.txt and the sub folders:

Warning: Failed opening '/online/online.php' for inclusion (include_path='.:/usr/local/lib/php') in /files/home1/douza/barrys/index.php on line 55

jpmoriarty
04-02-2003, 01:54 PM
try "require" instead of include, see if that helps

marshy28
04-02-2003, 02:25 PM
That just makes it fatal error......

Thanks anyway

jpmoriarty
04-03-2003, 03:56 AM
humm, thats not what we were after is it.

Can i just check that online/online.php runs fine on it's own (ie if you type the path into the address bar direct)?

pyro
04-03-2003, 08:14 AM
Perhaps your server has Registered Globals off. It is better to not use them, either way. Change these lines:

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

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

to:

$ip = $_ENV['REMOTE_ADDR'];
$time = time();
$minutes = 15;
$found = 0;
$users = 0;
$user = "";

$tmpdata = $_SERVER['DOCUMENT_ROOT']."/online/data";

marshy28
04-03-2003, 01:19 PM
Thanks pyro but its still not working.

I think its somthing to do with the files or directory's becuase it works fine on my web server but not when I upload to my web space.

I cant believe its causing me headache as its only a very some piece of php.

Please help me.

Thanks in advance Marshy

marshy28
04-03-2003, 01:27 PM
thankyou all for all your help but I have sorted it

Marshy

pyro
04-03-2003, 01:30 PM
Do we get to know what the problem ended up being?

marshy28
04-05-2003, 04:18 AM
Hi Pyro,

The problem was that I did not state the full path to the online.php file plus changing the code like you said also sorted my other problem.

But now the codes working but everytime I refresh the page it adds an other user on how do I prvent this from happenning to only show the total online users at any one time

Please find online.php below:

<?


$ip = $_ENV['REMOTE_ADDR'];
$time = time();
$minutes = 15;
$found = 0;
$users = 0;
$user = "";

$tmpdata = $_SERVER['DOCUMENT_ROOT']."/files/home1/douza/barrys/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 "<strong><font face=Comic Sans Ms color=red>Online Visitors:&nbsp;$users</font></strong>";
?>


Thanks Marshy

pyro
04-05-2003, 09:28 AM
It sounds like it's not finding the IP. Try echoing this to the screen $_ENV["REMOTE_ADDR"] and see what it returns...

marshy28
04-05-2003, 01:22 PM
I put the piece of code into echo.php and uploaded to the server and nothing happened? I dont know if I have echoed it correctly

<?php
echo $_ENV["REMOTE_ADDR"]
?>

Thanks

pyro
04-05-2003, 01:53 PM
That would be your problem, then. It should be echoing the users IP address. This is why it counts users every time they refresh.

marshy28
04-05-2003, 02:11 PM
How do I make it echo the IP then?

Is it a server problem or a coding fault?

Thanks

pyro
04-05-2003, 02:54 PM
It must be a server fault... If they have registerd globals turned on, you could try it like this.

<?PHP
echo $REMOTE_ADDR;
?>

but I'd think that if either of the two ways was going to work, it'd be the other one...

marshy28
04-05-2003, 03:17 PM
$ip = $REMOTE_ADDR;


This code worked fine and now only says 1 user online no matter how many times I refresh thanks pyro


one last point when you told me to change the line:
$ip = $REMOTE_ADDR; to $_ENV['REMOTE_ADDR'];

you also seaid to change the line:
$tmpdata = $DOCUMENT_ROOT to $tmpdata = $_SERVER['DOCUMENT_ROOT']


Does this bit need changing back too or should I leave it be

Thanks

pyro
04-05-2003, 03:39 PM
The difference between the two is this:

$_SERVER['DOCUMENT_ROOT'] (works when registered globals are on or off)
and
$DOCUMENT_ROOT (only works when registerd globals is on)

Also, I'm not sure why $_ENV["REMOTE_ADDR"] didn't work...That should be the preferred way of doing it...