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 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
[PHP] How to make a log file.
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Recently Posted "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
My Experiences So Far with Languages
Selecting Your First Programming Language
[PHP] - Lets kill the complex IF
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
Recently Rated "Programming" Articles
[PHP] - Lets kill the complex IF
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
Actionscript API
Actionscript Events
My Experiences So Far with Languages
Selecting Your First Programming Language
Vectors in C++
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (2)
Hardware (1)
Internet & Web Sites (3)
Life In General (2)
Networking (1)
Operating Systems (5)
Other (2)
Programming (47)
Security (10)
Software Development (5)
Web Development (14)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


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