A lot of people want to keep track of who visit their site. There are several ways to collect user data and the most easiest method would be to collect data using a third party company that logs IPs, page visited and other information such as web browser, data, time, etc. Then then data can be modify to create data on demographics and Internet service providers by extrapolating the IP addresses. However, what is fun of having a third party do your code for you? The fun part comes win when we can make our own code such as the one posted below:
<!--ip log--> <?php $v_ip = $_SERVER['REMOTE_ADDR']; $v_date = date("l d F H:i:s"); $v_page = $_SERVER['REQUEST_URI']; $u_ref = $_SERVER['HTTP_REFERER']; $fp = fopen("sanuja.com-ip_log.php", "a"); if ($v_ip != "192.168.1.1") { fputs($fp, "<strong>IP: $v_ip - </strong><strong>PAGE:</strong> $v_page - <strong>DATE:</strong> $v_date - <strong>REFERER:</strong> $u_refn "); } fclose($fp); ?>
The data not only collected locally and stored in your own server, but also you have the complete freedom to modify how the data is collected and stored. It is your code and the opportunities for improvements are limitless. The sample code above has several components where each one play a role in what type of data is being stored and how they were processed. First of all, keep in mind that this example is not a perfect one to use on your site because this was written as an example to demonstrate the capabilities of PHP coding. But this is a functional working code.
Let’s start from the first line of code. The very first thing you will notice is that you have to open a PHP code at < ?php, where entire code will be written between this and ?>. The $v_ip = $_SERVER[‘REMOTE_ADDR’]; is used to collect the IP address from the visitor. The variable name, $v_ip is arbitrary and as long as you abide by the rules of PHP variable naming such as no spaces can be used in the variable name. (example of an invalid variable would be $v ip because it has a space between the $v and ip.) The $_SERVER[‘REMOTE_ADDR’] is the PHP default IP logging code where the PHP module installed on your server will send a command to the main website to collect REMOTE_ADDR information, which is the IP address of the user.
The second line consist of $v_date = date(“l d F H:i:s”) will take the date and time from the host server(the time of the web server), when the visitor view the site. The format for date and time can be modified and if you like more information, please read this article at: Format a local time/date in PHP
The third line, $v_page = $_SERVER[‘REQUEST_URI’]; is written to record the pages that visitors opened. Every time someone open up a page, the page address will be recorded. However, the key word is page address NOT the URL, which is a good thing because this will prevent the server from creating large files with unwanted entire URL of the site. If you are bit confused, here is an example. If a visitor open the file http://www.sanuja.com/about.php the system will record it as about.php and omit the non local data path.
The forth one from the top is $u_ref = $_SERVER[‘HTTP_REFERER’] which will record the referral URL. The referral URL is the page where the link to a particular page was requested (aka clicked). This is where the visitor was directed either to this web site OR other parts or pages of this site within this site. For example, if a user came from Google search, it will record something similar to this http://www.google.ca/url?sa=t&rct=j&q=sanuja&source=web&cd=8&ved=0CDsQFjAH&url=http%3A%2F%2Fsanuja.com%2F&ei=fK_KTs6JJsmQiALovbHIDw&usg=AFQjCNFccQt3qiLADE-feGZafUNJOWVk1g or if it is from this site itself, it will record as /blog or index.php, /science/, science/mineral_properties.php, etc, etc.
The fifth line is a very important part of this code. The $fp = fopen(“sanuja.com-ip_log.php”, “a”) will create a file on the defined location or the current location, depend on how the code is written to store all the data that collected above. You can make a .php, .html, .txt or some other file format. However, it is recommended that you use minimum recourse consuming format to reduce the load on your server. The .txt format that will create a text file would be the best. Another thing to keep in mind is that make sure the file is NOT accessible by the general public. This is for your safety as well as privacy of others. The file should be either located in a separate folder that is not related to web folder path or the file should be restricted by permission denial or password.
The next part has to be explained with several in order to explain well. So, here we go…
if ($v_ip != "192.168.1.1") { fputs($fp, "<strong>IP:</strong> $v_ip - <strong>PAGE:</strong> $v_page - <strong>DATE:</strong> $v_date - <strong>REFERER:</strong> $u_refn "); }
The if statement was used to omit IP address of your own. Most routers and PHP installed on Windows servers will recognized your internal IP when you access the site from your own home network. The IP posted above the internal IP for the route which is 192.168.1.1, where it will NOT log the visit from this address. I believe that you can add more than one IP to omit from the data file, but I don’t have a clue how to do it. If you know how to do it, please post a reply with the code. The fputs($fp, ) is the code to enter the information in the file that we have created in line five. The variables that we created at the top of the code is now being used to point the data to the right location. All the variables starts from the character $ with the variable name immediately after that. The bold information is for you to keep track on data and will be loaded on a .php or .html/.htm file with clear indication on where once item of data begins and where it ends.
Now the down side of how the fputs($fp ) data code is written… As I noted at the beginning, this code is not a very good code but rather a good working example code. The reason is that all the data separating information with bold information such as IP: and the HTML code such as br, u, i, etc are all create more load to the server than needed. You can avoid this by simply creating a text file with no HTML tags or other additional data would be better.