Click to See Complete Forum and Search --> : PHP Merging Help


Jick
08-05-2003, 04:36 PM
I found this script which they say will tell you the current users online and also the users that have been on all day. Right now it's two seperate files. One called (online.php) and the other called (today.php). They each have a log file that they write their info to: (online.txt) and (today.txt). I'm pretty sure it's possible to merge these two scripts but still have them function the same way. I need help merging them please.

Here is (online.php):
<?PHP
$ip = $REMOTE_ADDR;
$time = time();
$minutes = 15;
$found = 0;
$usersnow = 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");
$usersnow = $usersnow + 1;
}
}

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

fclose ($f);
?>

And here is (today.php):
<?PHP
$ip = $REMOTE_ADDR;
$time = time();
$minutes = 60 * 24;
$found = 0;
$userstoday = 0;
$user = "";

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

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

$f = fopen("$tmpdata/today.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");
$userstoday = $userstoday + 1;
}
}

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

fclose ($f);
?>

And here my (index.php) file that displays the info:
<?PHP include ("online.php"); ?>
<?PHP include ("today.php"); ?>
Visitors Online Now: <?PHP echo "$usersnow"; ?><br>
Visitors Online Today: <?PHP echo "$userstoday"; ?>
You can also preview it by going to: http://kd7pyo.infinitypages.com/online/.

Exuro
08-05-2003, 11:28 PM
You could always just replace your two includes with the actual code from the files...

All you'd do is replace this line in your index file:

<?PHP include ("online.php"); ?>

With this:

<?PHP
$ip = $REMOTE_ADDR;
$time = time();
$minutes = 15;
$found = 0;
$usersnow = 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");
$usersnow = $usersnow + 1;
}
}

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

fclose ($f);
?>


And this line of the index file:

<?PHP include ("today.php"); ?>

With this:

<?PHP
$ip = $REMOTE_ADDR;
$time = time();
$minutes = 60 * 24;
$found = 0;
$userstoday = 0;
$user = "";

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

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

$f = fopen("$tmpdata/today.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");
$userstoday = $userstoday + 1;
}
}

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

fclose ($f);
?>

Jick
08-05-2003, 11:40 PM
Hey thats an idea. Thanks. I have one other question for you or Pyro. From looking at this script do you think it will do what they say? They say it will tell you how many users have been on for that whole day! If I remember right Pyro once told me that you cannot do this without crons. From looking at it myself I don't see anything about crons in this script yet they say it works. What do any of you guys think?

pyro
08-06-2003, 06:58 AM
What it appears like that script will do is tell you how many users have been on in a 24 hour period, not any particular day.