Coder Profile - Show off your skills, get a coder profile.
 
 
 
[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
  1. <?php
  2.  
  3.        $ip = $_SERVER['REMOTE_ADDR']; //Get there ip address.
  4.        $agent = $_SERVER['HTTP_USER_AGENT']; //Get there user agent, Firefox etc, and some other info about it.
  5.        $ref = $_SERVER['HTTP_REFERER']; // Referer, how they got to your website, who linked them, where they clicked that //link.
  6.        $date = date("H:i dS F"); //Get the date and time.
  7.        $file = "log.htm"; //Where the log will be saved.
  8. ?>
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
  1. $open = fopen($file, "a+"); //open the file, (log.htm).
  2. fwrite($open, "<b>IP Address:</b> " .$ip . "<br/>"); //print / write the ip address.
  3. fwrite($open, "<b>Referer:</b>". $ref . "<br/>"); //print / write the referer.
  4. fwrite($open, "<b>UserAgent:</b>". $agent. "<br/>"); //print / write thier useragent.
  5. fwrite($open, "<b>Date & Time:</b>". $date. "<br/>"); //print / write the date and time they viewed the log.
  6.  
  7. fclose($open); // you must ALWAYS close the opened file once you have finished.
If you now add all that together, your code should look like this...
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2.  
  3.        $ip = $_SERVER['REMOTE_ADDR']; //Get there ip address.
  4.        $agent = $_SERVER['HTTP_USER_AGENT']; //Get there user agent, Firefox etc, and some other info about it.
  5.        $ref = $_SERVER['HTTP_REFERER']; // Referer, how they got to your website, who linked them, where they clicked that //link.
  6.        $date = date("H:i dS F"); //Get the date and time.
  7.        $file = "log.htm"; //Where the log will be saved.
  8.  
  9.  
  10.  
  11.  
  12.                      $open = fopen($file, "a+"); //open the file, (log.htm).
  13.  
  14.                             fwrite($open, "<b>IP Address:</b> " .$ip . "<br/>"); //print / write the ip address.
  15.                             fwrite($open, "<b>Referer:</b>". $ref . "<br/>"); //print / write the referer.
  16.                             fwrite($open, "<b>UserAgent:</b>". $agent. "<br/>"); //print / write thier useragent.
  17.                             fwrite($open, "<b>Date & Time:</b>". $date. "<br/>"); //print / write the date and time they viewed the log.
  18.  
  19.                      fclose($open); // you must ALWAYS close the opened file once you have finished.
  20.  
  21.  
  22. ?>
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
  1. include('log.php');
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.
 
otoom     Posted 2.27 Years Ago
 
 
Forgot about them in articles to be quite franc.

EDITED!
 
VBAssassin     Posted 2.27 Years Ago
 
 
Surprised you didn't use [code] tags... ?

Kind regards,
Scott
 
Izzmo     Posted 2.43 Years Ago
 
 
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.
Page 1 of 1
More Articles By This Author
Quick Threading Tutorial - C++
First look at C
Cheat Sheet Pack
[PHP] How to make a log file.
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Ant (16)
United Kingdom, West Midlands
otoom has 8 fans
become a fan
» Applications
Articles Articles (9)
Source Codes Source Codes (18)
Code Pin Board Code Pin Board (2)
» About Me
About Me About Me
Portfolio Portfolio (2)
Friends Friends (159)
Interview Interview
» Misc
Overview Overview
Send A Friend Invite
Send Message Send A Message
RSS Whole Profile Feed
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
1.67 Year Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2010, Scott Thompson, All Rights Reserved