A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Log visitors with PHP & MySQLIn this tutorial you'll see how you can track your website's visitors and getting information such as user-agent or referrer. Also, we'll be using reverse DNS to find out more interesting information about his IP. |
On Saturday, October 1st 2005 at 04:41 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.8 with 5 votes) |
|||||
|
For this tutorial you'll need a PHP server and a MySQL database. First let's create a MySQL table where we will store information about the visitors:
We need these fields to store the IP of the visitor, the referrer, the URL he's currently visiting, the date of the visit and the user-agent (browser). From the PHP script you need to connect to the MySQL database using the appropriate credentials, and select the database where the logs table can be found:
Now on every page on which you want the visitor to be tracked, you include the following PHP line (you will probably want to insert it in header.php or some other file which you include in all your pages):
Here we simply insert data into the table. Let's see what the server variables ($HTTP_SERVER_VARS[]) can do: $HTTP_SERVER_VARS['REMOTE_ADDR'] - Gets the visitor's IP. $HTTP_SERVER_VARS['HTTP_REFERER'] - Gets the URL that referred the visitor to the current page; so if the visitor clicks your link from http://www.geekpedia.com, you will get http://www.geekpedia.com. This way you know where your visitors come from. $HTTP_SERVER_VARS['REQUEST_URI'] - The current URL that's being visited. $HTTP_SERVER_VARS['HTTP_USER_AGENT'] - The User-Agent gives information about the browser, plugins and extensions installed. Now what's left is retrieving data from the database and analyzing the visitors. We do this on another page (which you will probably want to password protect):
Many of the features of this script are optional. For example the one that highlights rows that were visited in the current day can easily be removed. This script will normally run fast, however there's something that's slowing it down a lot - the gethostbyaddr() function which does the reverse DNS on the IP. For those who don't know, reverse DNS helps you find out the domain from which the visitor is visiting (this can reveal the ISP, proxy, or company the user is visiting from). For example an IP such as 198.116.144.49 might not say much, however doing a reverse DNS will reveal nasa.gov. So if your script takes a lot to load when you have many visitors stored in the table, don't worry, it will eventually load but it's currently doing reverse DNS on each and every IP. This can be optimized a bit by skipping reverse DNS if the previous IP is the same as the current IP. Some folks may say reverse DNS can be done before entering the values in the database, however that will slow down the page loading when the visitors are browsing your website. Say we want to monitor visitors from Microsoft Corporation. We know their IPs reverse DNS to tidexxx.microsoft.com, where xxx is a number from 0 to 999, such as tide181.microsoft.com. Also, some IPs from Microsoft Corporation resolve to research.microsoft.com (their Research team). In the script we monitor these visitors using strstr(strtolower($Host), "microsoft") which checks to see if microsoft can be found in the resolve DNS string. microsoft.com should work even better. You can use this method to track the Google crawler, since the Google crawler resolves to domains such as crawl-66-249-64-28.googlebot.com, so using gooblebot.com instead of microsoft.com will bring you the desired result. The Google crawler and several other crawlers can also be tracked by the user agent (Googlebot/2.1 (+http://www.google.com/bot.html)). You will need to delete these logs from time to time or they'll get really big. You can use a cron job, or you can do it manually by placing the following line in a different page which you can access to empty the table:
Here's how the page looks like when having 3 visits stored in the table made by 2 visitors (one visited a page, and another visited 2 pages): |
||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||||
|
||||||
Current Commentsgood work
Works great!
Thanks - excellent ideas, works well, sure a lot of people poke around in web sites!
Not secure enough...
As I said in the tutorial, you should password protect your page. You can easily do that using htaccess.
Is there anyway to get a script like this to detect referrals with target=_blank?
great
i tried this script but it doesnt seem to log the visitors ip......it only collects the time they visited and nothing else......i would appreciate any help thanx
I get the error in command ") TYPE=MyISAM ;" when i put it in mysql query
This script works and gets the IP of the visitor!
However, even though it displayed the headers
ID,IP, HOST etc in the tables, the data from the sql table are not queried since i get the following message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /logs/logs.php on line 63
this corresponds to the code line : while($LogF = mysql_fetch_array($LogQ))
it might be a mispelling or something? cant find though.. i would appreciate any help!
it is cool.
but what i want is the one that when a visitor comes to my site and registers for my referer program he would be given a code with a unique id like www.kojexconsult.com?id=4321 and when he gives the link to another visitor and they click on it it would register on my database and then i remunerate the referer
Nice information really cool :)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(`VisID`)
) TYPE=MyISAM' at line 16
That's error on the sql
When i'm trying to create the table it gives me the following error:
Syntax error near '(`VisID`)) TYPE = MyISAM' at line 16
464
Related Tutorials
Related Source Code
PHP Job Search