PHP +mysql based on IP to determine the user's city program

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags based database password ip ips link mysql mysql tutorial name

PHP tutorial based on IP to determine the user's city program
$servername = ' localhost ';
$dbname = ' 8684 ';  //Database tutorial name
$dbusername = ' Root '; Database user name
$dbpassword = ' 123456 ';//Database Password
 
$link =mysql Tutorial _connect ($servername, $dbusername, $ Dbpassword);
mysql_select_db ($dbname) or Die ();
mysql_query ("Set names ' GBK '");

function Get_real_ip () {
$ip =false;
if (!empty ($_server["HTTP_CLIENT_IP"])) {
$ip =$_server["Http_client_ip"];
}
if (!empty ($_server[' http_x_forwarded_for ')) {
$ips Tutorial =explode (",", $_server[' http_x_forwarded_for '));
if ($IP) {
Array_unshift ($ips, $IP);
$ip =false;
}
For ($i =0 $i <count ($ips); $i + +) {
if (!eregi ("^ (10│172.16│192.168).", $ips [$i])) {
$ip = $ips [$i];
Break;
}
}
}
$ip = $ip $ip: $_server[' remote_addr '];
List ($ip 1, $ip 2, $ip 3, $ip 4) =explode (".", $ip);
Return $ip 1*pow (256,3) + $ip 2*pow (256,2) + $ip 3*256+ $ip 4;
}
$ip =get_real_ip ();
$sql = "SELECT * from IP where start<= $ip order by start DESC limit 1";
$q =mysql_query ($sql);
$rs =mysql_fetch_array ($q);
$city = $rs ["City"];

Echo $city;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.