php to obtain the client ip address

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags access address client code echo function get global variables

php tutorial to obtain the client ip address This tutorial provides several code to obtain the ip address, each has its own advantages, users get ip is the use of global variables php to achieve, the following to see if you are looking for.
* /
// The easiest way

$ ip = $ _server ["remote_addr"];
Echo $ ip; / / Most useful to obtain the user ip address code

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;
}
}
}
return ($ ip? $ ip: $ _server ['remote_addr']);
}
echo get_real_ip (); / / Get ip address and port number

$ ip = $ _ server ["remote_addr"];
$ port = $ _server ['remote_port'];
Echo $ ip. $ port; / / A little meaning

if (getenv ('http_client_ip'))
{
$ onlineip = getenv ('http_client_ip');
}
elseif (getenv ('http_x_forwarded_for'))
{
$ onlineip = getenv ('http_x_forwarded_for');
}
elseif (getenv ('remote_addr'))
{
$ onlineip = getenv ('remote_addr');
}
else
{
$ onlineip = $ http_server_vars ['remote_addr'];
}
echo $ onlineip;
echo "
"; / / Use qq interface

function get_ip_place () {
$ ip = file_get_contents ("http://fw.qq.com/ipaddress");
$ ip = str_replace ('', '', $ ip);
$ ip2 = explode ("(", $ ip);
$ a = substr ($ ip2 [1], 0, -2);
$ b = explode (",", $ a);
return $ b;
}
$ ip = get_ip_place ();
print_r ($ ip);

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.