Page 1 of 1

Using a LOG.HTML file

Posted: Sun Nov 30, 2003 1:04 pm
by hendy
i have created a log.html file, everytime a user logs on, it records their IP address, time and date. Next I need to use PHP to check the log file everytime someone logs ontot he site, to see if they have been a previous visitor, if they have, i need to stop the counter from incrementing, how can this be done???

thanks

Posted: Sun Nov 30, 2003 1:17 pm
by mchaggis
it all depends on the format of your LOG.HTML file?

Need more info really, like the code that writes the log

Posted: Sun Nov 30, 2003 11:52 pm
by hendy
<?
$time = date("F jS Y, h:iA"); //using the date() function
$ip = $REMOTE_ADDR;
//$remote_addr is PHP variable to get ip address
$referer = $HTTP_REFERER;
//$http_referer is PHP variable to get referer
$browser = $HTTP_USER_AGENT;
//$http_user_agent is PHP variable for browser

$fp = fopen("log.html", "a");
//use the fopen() function
fputs($fp, "Time: $timeIP:
$ipReferer: $refererBrowser: $browser");
//using the fputs() function
fclose($fp);
//closing the function
?>


i think this will work, but i dont know how to write a peice of code that will search this log file for a previous log in from an IP address

Posted: Sun Nov 30, 2003 11:57 pm
by infolock
just a question... why aren't you using a database for this ? like mysql.

Posted: Mon Dec 01, 2003 9:23 am
by mchaggis
Ok, you really want to be storing the username as well then in the log, otherwise you can only check the ip address of the visitor... so if then:

Code: Select all

<?
$Arr = file("log.html");
$returning = FALSE;
foreach($Arr as $line) {
    if (ereg("^$username:.*$', $line) $returning = TRUE;
}
unset ($Arr);
?>
You could always use a cookie also?

Posted: Tue Dec 02, 2003 8:31 am
by hendy
i dont really need a username, the plan is to have the log.html file storing there ip address and date, so the next time someone logs on from a pc using an ip address already in the log file, the counter will not increment.
Im kinda stuck here, im just new to php, so forgive my ignorance if i have done something stupid!!!!

cheers

Posted: Tue Dec 02, 2003 9:08 am
by mchaggis
Ok, then the code ur looking for is:

Code: Select all

<?
$Arr = file("log.html");
$returning = FALSE;
foreach($Arr as $line) {
    # Split the line read in
    $tmpArr = split(':', $line);
    # Looking at code above, Array el 2 contains IP
    if ($Arr[2] == $REMOTE_ADDR) $returning = TRUE;
}
unset ($Arr);
?>
You can the use the var $returning to see if you need to log... Btw, I wouldn't store the spaces in the log

SOLVED how to use a LOG.HTML file

Posted: Fri Dec 05, 2003 9:15 am
by hendy
Thanks for all ur help, its really been appreciated

Posted: Fri Dec 05, 2003 10:23 am
by Derfel Cadarn
hendy wrote:i dont really need a username, the plan is to have the log.html file storing there ip address and date, so the next time someone logs on from a pc using an ip address already in the log file, the counter will not increment.
But visitors with a dynamic IP-number (given by their ISP) will not be detected in doing so! Or is that what you want?

Posted: Fri Dec 05, 2003 11:24 am
by BDKR
hendy wrote:<?
$time = date("F jS Y, h:iA"); //using the date() function
$ip = $REMOTE_ADDR;
//$remote_addr is PHP variable to get ip address
$referer = $HTTP_REFERER;
//$http_referer is PHP variable to get referer
$browser = $HTTP_USER_AGENT;
//$http_user_agent is PHP variable for browser

$fp = fopen("log.html", "a");
//use the fopen() function
fputs($fp, "Time: $timeIP:
$ipReferer: $refererBrowser: $browser");
//using the fputs() function
fclose($fp);
//closing the function
?>


i think this will work, but i dont know how to write a peice of code that will search this log file for a previous log in from an IP address
Something you may want to consider is making sure that each new entry writes to a new line. Especially considering that one of the examples given on how to parse the file is using the file() function. The file function adds elements to an array based on new lines.

So....

Code: Select all

fputs($fp, "Time: $timeIP: $ipReferer: $refererBrowser: $browser\n");


...would be better. Notice the '\n' on the end. That tells PHP that anything to be written beyond this point needs to be put on a new line.

Another thing you may want to consider is either avoiding spaces when writing the file,

Code: Select all

# Example with no spaces between delimiters
fputs($fp, "Time:$timeIP:$ipReferer:$refererBrowser:$browser\n");
or triming the elements of the exploded (I think the example above actually uses split()) string later on. However, that requires more in the way of code. I would suggest building your string to write out to the page without any spaces around the delimiters, which are ":" in this case.

Cheers,
BDKR