PHP 5 ways to get user access to IP address _php tips

Source: Internet
Author: User
Tags learn php learn php programming php programming

This article for you to share 5 kinds of PHP access to the User IP address methods for your reference, the specific content as follows

<?php//Method 1: $ip = $_server["REMOTE_ADDR"];
Echo $ip; Method 2: $user _ip = ($_server["Http_via"])?
$_server["Http_x_forwarded_for"]: $_server["REMOTE_ADDR"]; $user _ip = ($user _ip)?
$user _ip: $_server["REMOTE_ADDR"];
echo $user _ip;
  Method 3:function Getrealip () {$ip =false;
  if (!empty ($_server["HTTP_CLIENT_IP"])) {$ip = $_server["Http_client_ip"];
    } if (!empty ($_server[' http_x_forwarded_for ')) {$ips = 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];
Return ($ip $ip: $_server[' remote_addr '));
Echo Getrealip (); Method 4:if ($HTTP _server_vars["Http_x_forwarded_for"]) {$ip = $HTTP _server_vars["http_x_forwarded_for"]; ElseIf ($HTT p_server_vars["Http_client_ip"]) {$ip = $HTTP _server_vars["Http_client_ip"]; } elseif ($HTTP _server_vars["REMOTE_ADDR"]) {$ip = $HTTP _server_vars["REMOTE_ADDR"]; ElseIf (getenv ("Http_x_forwarde 
D_for ")) {$ip = getenv (" Http_x_forwarded_for ");} elseif (Getenv (" Http_client_ip ")) {$ip = getenv (" Http_client_ip ");
} elseif (getenv ("REMOTE_ADDR")) {$ip = getenv ("REMOTE_ADDR");} else {$ip = "Unknown";} Echo $ip;
  Method 5: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_ad
DR '];  Echo $onlineip;

The above is the entire content of this article, I hope that you learn PHP programming help.

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: 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.