Using a LOG.HTML file
Moderator: General Moderators
Using a LOG.HTML file
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
thanks
<?
$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
$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
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:
You could always use a cookie also?
Code: Select all
<?
$Arr = file("log.html");
$returning = FALSE;
foreach($Arr as $line) {
if (ereg("^$username:.*$', $line) $returning = TRUE;
}
unset ($Arr);
?>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
Im kinda stuck here, im just new to php, so forgive my ignorance if i have done something stupid!!!!
cheers
Ok, then the code ur looking for is:
You can the use the var $returning to see if you need to log... Btw, I wouldn't store the spaces in the log
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);
?>SOLVED how to use a LOG.HTML file
Thanks for all ur help, its really been appreciated
- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
But visitors with a dynamic IP-number (given by their ISP) will not be detected in doing so! Or is that what you want?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.
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.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
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");Cheers,
BDKR