Php code for obtaining the client's internet/public IP address _ PHP Tutorial

Source: Internet
Author: User
Php code for obtaining the Internet IP address of the client. Leng Feng: both methods are available. The first method must be supported by the built-in curl, and the second method is more common. The second type is recommended. Method 1: curlfunctionget_onlineip () {$ chcurl_init Cold front: both methods are available. The first method must be supported by built-in curl, and the second method is more common. The second type is recommended.
Method 1: curl

Function get_onlineip (){
$ Ch = curl_init (http://www.ip138.com/ip2city.asp );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ A = curl_exec ($ ch );
Preg_match (/[(. *)]/, $ a, $ ip );
Return $ ip [1];
}

Method 2: $ _ SERVER [HTTP_X_FORWARDED_FOR] to obtain the corresponding address

Function get_onlineip (){
$ Onlineip =;
If (getenv (HTTP_CLIENT_IP) & strcasecmp (getenv (HTTP_CLIENT_IP), unknown )){
$ Onlineip = getenv (HTTP_CLIENT_IP );
} Elseif (getenv (HTTP_X_FORWARDED_FOR) & strcasecmp (getenv (HTTP_X_FORWARDED_FOR), unknown )){
$ Onlineip = getenv (HTTP_X_FORWARDED_FOR );
} Elseif (getenv (REMOTE_ADDR) & strcasecmp (getenv (REMOTE_ADDR), unknown )){
$ Onlineip = getenv (REMOTE_ADDR );
} Elseif (isset ($ _ SERVER [REMOTE_ADDR]) & $ _ SERVER [REMOTE_ADDR] & strcasecmp ($ _ SERVER [REMOTE_ADDR], unknown )){
$ Onlineip = $ _ SERVER [REMOTE_ADDR];
}
Return $ onlineip;
}

Echo get_onlineip ();
?>

Bytes. The second type is recommended. Method 1: curl function get_onlineip () {$ ch = curl_init...

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.