| Save IP address and referrer of our page |
Let's suppose we want to record the IP address and Referrer of all visitors to our page. The script bellow may be placed within the code of our page and it will create two files to store both data: "ips_file.txt" to store IP addresses and "ref_file.txt" to store referrers. Additionally, this script may be use to track number of times users from a specific IP address visits our page.
To use this code written permission must be available in the directory containing the file with this code (in order to create the files).
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <? // save ip $ip=$_SERVER["REMOTE_ADDR"]; if ($ip!=""){ $ipfile= "ips_file.txt"; $allips = file_get_contents ($ipfile); if (strpos($allips," $ip ")>0){ $allips = preg_replace ("/ $ip /"," $ip x",$allips); $tempvar= fopen($ipfile, "w"); fwrite ($tempvar, $allips); fclose($tempvar); }else{ $tempvar = fopen($ipfile, "a"); fwrite ($tempvar, " $ip x\n"); fclose($tempvar); } } // save referrer $ref=$_SERVER["HTTP_REFERER"]; if (strpos($ref,"mydomain.com")==0 and $ref!=""){ $refffile= "ref_file.txt"; $tempvar = fopen($refffile, "a"); fwrite ($tempvar, $ref."\n"); fclose($tempvar); } ?> |
Lines 2-13: saves the IP addresses to the file "ips_file.txt". Check the typical content of this file below.
Line 3: Get the IP address of visitors and stores the value in variable $ip.
Line 4: If $ip has been obtained, lines 5-16 are processed.
Line 5: Defines the name of the file containing the IP addresses.
Line 6: Reads the content of the file to variable $allips.
Line 7: Checks whether the IP of visitor has been already save in the file. If so, lines 8-11 are processed. If not, lines 13-15 are processed.
Lines 8-11: In line 8, a replacement in the variable containing the content of the file "ips_file.txt" is performed (variable $allips). This replacement will place a “x” after the IP address already recorded in the variable. In lines 13-15 the file "ips_file.txt" is overwritten with the content in the variable.
Line 4: If $ip has been obtained, lines 5-16 are processed.
Line 5: Defines the name of the file containing the IP addresses.
Line 6: Reads the content of the file to variable $allips.
Line 7: Checks whether the IP of visitor has been already save in the file. If so, lines 8-11 are processed. If not, lines 13-15 are processed.
Lines 8-11: In line 8, a replacement in the variable containing the content of the file "ips_file.txt" is performed (variable $allips). This replacement will place a “x” after the IP address already recorded in the variable. In lines 13-15 the file "ips_file.txt" is overwritten with the content in the variable.
The typical content of file "ips_file.txt" will be the one bellow, where the number of "x" after each IP indicates number of visits from that specific IP to our file.
| ips_file.txt |
| 150.150.150.150 x 150.150.150.151 xxxxxxx 150.150.150.152 xxx 150.150.150.153 x 150.150.150.154 xxxxxxxxx |
Lines 19-26: saves the referrer to the file "ref_file.txt". Check the typical content of this file below.
Line 20: Get the referrer of visitor and stores the value in variable $ref.
Line 21: The following are checked:
The typical content of file "ref_file.txt" will be the one bellow:
Line 21: The following are checked:
Check whether the referrer site is our own site (whether "mydomain.com" is within the variable $ref); if so, next three lines are not processed.
Check whether the value of $ref is different to "" (so whether a value exists); if no value is contained in the variable $ref, next three lines are not processed.
Line 22-25: the value at $ref is appended to file ref_file.txt.Check whether the value of $ref is different to "" (so whether a value exists); if no value is contained in the variable $ref, next three lines are not processed.
The typical content of file "ref_file.txt" will be the one bellow:
| ref_file.txt |
| http://search.yahoo.com/search?p=myquery http://www.myreferrer.com/main.html http://search.yahoo.com/search?p=myquery2 http://www.google.com/search?q=a+diferent+query http://www.myreferrer2.com/dir/file33.html |