Using a LOG.HTML file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hendy
Forum Newbie
Posts: 24
Joined: Sat Nov 29, 2003 6:35 am
Location: UK

Using a LOG.HTML file

Post 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
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

it all depends on the format of your LOG.HTML file?

Need more info really, like the code that writes the log
hendy
Forum Newbie
Posts: 24
Joined: Sat Nov 29, 2003 6:35 am
Location: UK

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

just a question... why aren't you using a database for this ? like mysql.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post 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?
hendy
Forum Newbie
Posts: 24
Joined: Sat Nov 29, 2003 6:35 am
Location: UK

Post 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
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post 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
hendy
Forum Newbie
Posts: 24
Joined: Sat Nov 29, 2003 6:35 am
Location: UK

SOLVED how to use a LOG.HTML file

Post by hendy »

Thanks for all ur help, its really been appreciated
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Post Reply