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


Posted By otoom
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
otoom     Posted 223 Days Ago
 
 
Forgot about them in articles to be quite franc.

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

Kind regards,
Scott
 
Izzmo     Posted 285 Days 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
[PHP] How to make a log file.
[VB] Hello World
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Recently Posted "Programming" Articles
[PHP] - Lets kill the complex IF
Java: Obtaining User Input - Part 1
Basic PortScanner in VB6.0
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
Recently Rated "Programming" Articles
[PHP] - Lets kill the complex IF
Java: Obtaining User Input - Part 1
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Networking (1)
Operating Systems (4)
Other (2)
Programming (51)
Security (10)
Software Development (5)
Standards (1)
Web Development (15)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
What would you rate the usability of Coder Profile?

9 to 10
7 to 8
5 to 6
3 to 4
1 to 2


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