How to insert visitor IP address and referrer in a tableWhen visitors come to our website we can collect visitors IP address, referrer, browser details and others details and store them in a MySQL table. Note that this is one of the examples of storing some minimum details of the visitors and not a full script to collect all details. We will try to log minimum important details of the visitors like ip address, referrer, browser details, time of page access and a unique page name. We will keep one unique page name for each page as we will be using same table for all the pages of the site. So the variable to collect the unique page name of the page is assign different values in different pages.
Storing as decimal numberTo store (IPv4) IP address in a field we have to use one varchar field with length 16 as a typical address of internet standard dotted format will be like this. 126.96.36.199 . Note that we can convert this address to decimal equivalent value and store them if we want to do some further processing of the address. The four blocks of octal numbers of an IP address has to be converted to its decimal equivalent by using each block of four blocks of octal numbers. To do this PHP has a built in function ip2long which will take the IP address and return the decimal equivalent of this.
Here is an example.
We can restore the original octal values by using long2ip function. Here is an example.
Using MySQL inet_aton functionFinally if we are using MySQL to store IP address then we can use MySQL built in function inet_aton to convert IP address to binary value and store them.
The output will be 995976892
We can restore our IPv4 address by using inet_ntoa function in MySQL query like this
The output will be 188.8.131.52
Table Structure & ScriptIn this example we will store standard dotted format by using a varchar field but you can use integer field after converting the IP address to decimal number as we discussed above.
Field Size: To store IPV4 address we can keep the field length as 15 ( 4x3 plus 3 separators ). To store IPV6 address we have to keep 39 as field length ( 4x8 plus 7 separators ). To map IPV4 to IPV6 address you have to keep 45 as field length. At present we can go with IPV4 address lengthStoring visitor details in MySQL table We will store details in a database table after collecting the variable. The table structure you can download from here. Here are the details.
Getting referrerRead more on visitor Referrer
Collecting browser details
Getting IP addressRead more on visitor IP address
Tracking pageWe can store tracking page name by using server variable. If you are using any unique code for each page then that also you can store
Storing time of visitThere is a field dt which stores the date and time of visit. This is done by using automating adding date and time while adding the record.
You can read how to insert data to mysql table here. This code is kept inside a footer file or a common file which is used in all pages. So if you are not connected already to database then you can use database connection string to establish connection.
We have used one if condition checking to insert record if referrer of the visitor is present. We will also check that referrer is not internal pages of the site.
Here is the complete code
The above code will insert the visitor details to the table name track in mysql database.
You can read the article on reading data from table to display the data of the visitors in a page.
Storing visitor details using CSV file.Details can be stored in Comma Separated Value ( .csv ) file. Instead of storing in a MySQL database we can store them in a csv file. We will use fputcsv() PHP function to store data in a csv file.
Here is the code.
By using fopen function we are opening a file, if the file is not already created then it will create the file. The mode of opening the file is a, so the pointer will remain at the end of the file. So each time a new visitor comes to the page , a new line will be added at the end.
Storing data in day wiseYou can create one csv file for each day. For this change the file name like this.
Now you will get one file for each day storing your visitors details.
To display or read csv file you can use PHP function fgetcsv()
Here is the dump of the table track
This article is written by plus2net.com team.