Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post COME TO VENT NAO!!
Today 01:00 PM
Last post by qtpie
Today 05:39 PM
4 Replies, 5 Views
Go to first new post [S] Awesome Very Young...
09-04-2008 04:00 PM
Last post by Lanbo
Today 05:39 PM
9 Replies, 10 Views
Go to first new post Selling a few pets for NP
Yesterday 09:06 AM
Last post by matthew.n
Today 05:33 PM
3 Replies, 4 Views
Go to first new post Good AB list for newbies
09-03-2008 08:28 PM
Last post by Lanbo
Today 05:32 PM
7 Replies, 8 Views
Go to first new post [B] FQD!
Today 05:24 PM
Last post by NextGenWarrior
Today 05:24 PM
0 Replies, 1 Views
Reply
 
LinkBack Thread Tools Display Modes

 Recording Visitors (And Infomation About Them)
Old 10-18-2006, 03:46 PM   #1 (permalink)
Oosband
Guest
 
Posts: n/a
iTrader: / %
Recording Visitors (And Infomation About Them)

Stats have become really important over the past few years, businesses are investing thousands of dollars into knowing who visits their sites, and for good reason.

I'm going to show you a simple way of recording data about your visitors.

Firstly, open up NotePad and create a blank document, save it as visits.txt.
Upload the file and then chmod it to 777 - this means we can write to the file...

Now for the code that records visitor data...

Lets begin by opening the file and setting the variables:

Code:
<?php
$dtime = date('F jS Y, h:iA');
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($ip);
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ref = $_SERVER['HTTP_REFERER'];
Explination Of Variables:
$agent - The visitor's Web Browser
$uri - The URL they are viewing
$ip - The visitor's IP Address
$ref - The reffering URL
$host - The visitor's Host
$dtime - Date and Time of visit

---

Now, a little bit of error handling...
If a field is left blank, we're going to replace it with "None Recorded".

Code:
if($dtime == ""){
$dtime = "None Recorded";
}
if($ip == ""){
$ip = "None Recorded";
}
if($host == ""){
$host = "None Recorded";
}
if($agent == ""){
$agent = "None Recorded";
}
if($uri == ""){
$uri = "None Recorded";
}
if($ref == ""){
$ref = "None Recorded";
}
---

Now we're going to set what is to be written in our text file.
Code:
$entry_line = "
Date: $dtime
IP: $ip
Host: $host
Browser: $agent
URL: $uri
Referrer: $ref
---------------
";
This will simply write all the collected infomation and then seperate each entry with ---------------.

---

All we now need to do is open the txt file, write the entry, close the text file and then close the script...sound difficult? It really isn't!

Code:
$fp = fopen("visitors.txt", "a");
fputs($fp, $entry_line);
fclose($fp);
?>
fopen opens the specified file.
fputs writes to the file.
fclose closed the file.
?> ends the script.

---

Here's the full script: (All code together)

Code:
<?php
$dtime = date('F jS Y, h:iA');
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($ip);
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ref = $_SERVER['HTTP_REFERER'];

if($dtime == ""){
$dtime = "None Recorded";
}
if($ip == ""){
$ip = "None Recorded";
}
if($host == ""){
$host = "None Recorded";
}
if($agent == ""){
$agent = "None Recorded";
}
if($uri == ""){
$uri = "None Recorded";
}
if($ref == ""){
$ref = "None Recorded";
}

$entry_line = "
Date: $dtime
IP: $ip
Host: $host
Browser: $agent
URL: $uri
Referrer: $ref
---------------
";
$fp = fopen("visitors.txt", "a");
fputs($fp, $entry_line);
fclose($fp);
?>
Save it as visitors.php and use php include on each page to record visitor data from that page.

PHP Include:
Code:
<?php include("visitors.php"); ?>
---

Use this data wisely. I reccomend that you have a "Privacy Policy" describing the fact that you are collecting this data and that you wont be sharing it with any third partys ;)
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 05:39 PM.


vBulletin skin developed by: eXtremepixels
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.09757805 seconds (100.00% PHP - 0% MySQL) with 19 queries