note 89045 added to function.getenv
| From: | estarapapax at gmail dot com | Date: | Thu, 19 Feb 2009 05:19:16 +0000 |
| Subject: | note 89045 added to function.getenv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150845@lists.php.net to get a copy of this message | ||
This is a sample function for checking if your visitor comes from this certain country. This is
especially useful for amateur webmasters who don't want to use sql databases.
Sample use of function:
<?php
if(check_coutry('http://www.domain.com/files/philippines.csv')
=== true)
echo 'You are from the Philippines';
?>
Basically, you'll need CSV (or TXT) which lists the IP ranges of a certain country. Example
(excerpt of the philippines.csv only):
58.69.0.0, 58.69.255.255
58.71.0.0, 58.71.127.255
61.9.0.0, 61.9.127.255
61.14.28.0, 61.14.28.63
61.14.41.136, 61.14.41.143
and so on until the end
You may obtain these CSV files in a number of websites out there. Be sure to update these CSV files
regularly as these IP ranges change from time to time.
Here is the function:
<?php
function check_country($url_csv){
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url_csv);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$str_fromfile = curl_exec($curl_handle);
curl_close($curl_handle);
$range = explode("\n", $str_fromfile);
$ip_addr = getenv(REMOTE_ADDR); //gets the IP of the visitor
$ip_byte = explode('.', $ip_addr);
$ip_number = (16777216 * (int) $ip_byte[0]) + (65536 * (int) $ip_byte[1]) + (256 * (int)
$ip_byte[2]) + ((int) $ip_byte[3]);
for($i = 0; $range[$i] != NULL && $is_positive == NULL; $i++){ // the condition $line[$i] !=
NULL means that you should not put blank lines before the end of your CSV. The values should start
at line 1.
$range[$i] = rtrim(ltrim($range[$i])); //you may remove this if you are sure the CSV doesnt
contain whitespaces
$ends_addr = explode(',', $range[$i]); //for CSV (comma-separated values), comma is
the separator. You may change this if your TXT uses different separator.
$ends_addr[0] = rtrim($ends_addr[0]); //again, you may remove this if your CSV is free from
whitespaces
$ends_addr[1] = ltrim($ends_addr[1]); //yet, again
$start_ip_byte = explode('.', $ends_addr[0]);
$end_ip_byte = explode('.', $ends_addr[1]);
$start_ip_number = (16777216 * (int) $start_ip_byte[0]) + (65536 * (int) $start_ip_byte[1]) +
(256 * (int) $start_ip_byte[2]) + ((int) $start_ip_byte[3]);
$end_ip_number = (16777216 * (int) $end_ip_byte[0]) + (65536 * (int) $end_ip_byte[1]) + (256 *
(int) $end_ip_byte[2]) + ((int) $end_ip_byte[3]);
if($ip_number >= $start_ip_number && $ip_number <= $end_ip_number)
$is_positive = 1;
}
if($is_positive == 1)
return true;
else
return false;
}
----
Server IP: 202.90.158.134
Probable Submitter: 202.92.128.31
----
Manual Page -- http://www.php.net/manual/en/function.getenv.php
Edit -- https://master.php.net/note/edit/89045
Del: integrated -- https://master.php.net/note/delete/89045/integrated
Del: useless -- https://master.php.net/note/delete/89045/useless
Del: bad code -- https://master.php.net/note/delete/89045/bad+code
Del: spam -- https://master.php.net/note/delete/89045/spam
Del: non-english -- https://master.php.net/note/delete/89045/non-english
Del: in docs -- https://master.php.net/note/delete/89045/in+docs
Del: other reasons-- https://master.php.net/note/delete/89045
Reject -- https://master.php.net/note/reject/89045
Search -- https://master.php.net/manage/user-notes.php