[PHP] How to make a log file.
Programming
|
DoxCoding.com
Well, this will be more like a walk through, rather than a tutorial / article, Whatever. 
So, to make a log file, we need to use a great language, server sided, called PHP.
PHp is a very powerful server side language.
So, to get started.
We will make some declarations.
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
<?php $ip = $_SERVER['REMOTE_ADDR']; //Get there ip address. $agent = $_SERVER['HTTP_USER_AGENT']; //Get there user agent, Firefox etc, and some other
info about it. $ref = $_SERVER['HTTP_REFERER']; // Referer, how they got to your website, who linked
them, where they clicked that //link. $date = date("H:i dS F"); //Get the date and time. $file = "log.htm"; //Where the log will be saved. ?>
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
Well, that is the basic declarations.
I have commented them to help you on what they are / mean. Rather than explaining each of them.
Okay, lets move swiftly on.
We need to open the log file, print in the information, every time someone visits that log file.
So, lets do it.
Again, nice an simple. Commented work.
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
$open = fopen($file, "a+"); //open the file, (log.htm). fwrite($open, "<b>IP Address:</b> " .$ip . "<br/>"); //print /
write the ip address. fwrite($open, "<b>Referer:</b>". $ref . "<br/>"); //print /
write the referer. fwrite($open, "<b>UserAgent:</b>". $agent. "<br/>"); //print /
write thier useragent. fwrite($open, "<b>Date & Time:</b>". $date. "<br/>");
//print / write the date and time they viewed the log. fclose($open); // you must ALWAYS close the opened file once you have finished.
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
If you now add all that together, your code should look like this...
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
<?php $ip = $_SERVER['REMOTE_ADDR']; //Get there ip address. $agent = $_SERVER['HTTP_USER_AGENT']; //Get there user agent, Firefox etc, and some other
info about it. $ref = $_SERVER['HTTP_REFERER']; // Referer, how they got to your website, who linked
them, where they clicked that //link. $date = date("H:i dS F"); //Get the date and time. $file = "log.htm"; //Where the log will be saved. $open = fopen($file, "a+"); //open the file, (log.htm). fwrite($open, "<b>IP Address:</b> " .$ip . "<br/>");
//print / write the ip address. fwrite($open, "<b>Referer:</b>". $ref . "<br/>"); //print
/ write the referer. fwrite($open, "<b>UserAgent:</b>". $agent. "<br/>");
//print / write thier useragent. fwrite($open, "<b>Date & Time:</b>". $date. "<br/>");
//print / write the date and time they viewed the log. fclose($open); // you must ALWAYS close the opened file once you have finished. ?>
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
Save that as log.php.
If you now add an include into a page you wish this to be under. For example. The index page.
Within index.php
At the very top. Add...
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
That way, when they visit your index page, they are also viewing the log.php page.
Leaving you with a log.
Adding and managing this log.php script. You can do many other things, See what they are clicking, links wise, etc.
Like i said above, PHP is very powerful.
Kind regards,
Ant
Please login to rate coding articles.
Click here to register a free account with us.
|
|
Comments
|
| Please login to post comments. |
|
|
Forgot about them in articles to be quite franc.
EDITED!
|
|
|
Surprised you didn't use [code] tags... ?
Kind regards,
Scott
|
|
|
To expand, you might want to integrate this into a database. This would create way to much overhead, and you would also lose out on a lot on some logging because the file would be "locked" while it writes to it, and if someone hits the site at the same time, it would not write to it. Too much I/O would be commensing in this, which would make writing them to a relational database ideal.
Also, Apache and ISS (Microsoft) does all this by default in their logging, so it makes doing this pointless.
|
|
|
 |
Categories |
 |
|
|