Get real IP address and IP address verification functions in PHP

Source: Internet
Author: User
Tags php form php form validation
This article introduces two functions one is to verify the IP address, the other is to obtain the user's real IP address of the function, is also commonly used two IP operation function, the need for students can refer to.

1. Get the user's real IP address

function get_client_ip () {if (getenv ("REMOTE_ADDR") && STRCASECM                 P (getenv ("REMOTE_ADDR"), "Unknown")) {$onlineip = getenv ("REMOTE_ADDR");         return $ONLINEIP; } if (Isset ($_server[' remote_addr ')) && $_server[' remote_addr '] && strcasecmp ($_server[' REMOTE                 _addr '], "unknown") {$onlineip = $_server[' remote_addr '];         return $ONLINEIP;                 } if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "unknown") {                 $onlineip = getenv ("Http_client_ip");         return $ONLINEIP;         } if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown"))         {$onlineip = getenv ("Http_x_forwarded_for"); } return $ONLINEIP; }

Determine if an IP address

function Is_ip ($IP) {         $IP _array = Explode (".", $IP);         $IP _array_num = sizeof ($IP _array);         if ($IP _array_num! = 4)         {                 return FALSE;         }         $I = 0;         for (;    $I < $IP _array_num;    + + $I    )         {                 if (!is_numeric ($IP _array[$I]) && $IP _array[$I] < 0 | | 255 < $IP _array[$I]) 
  
   {                         return FALSE;                 }                 if (! ($I = = 3) &&! ($IP _array[$I] = = 255))                 {                         continue;                 }                 return FALSE;         }         return TRUE; }
  

Related articles:

PHP Verification of ID number correctness function

PHP ID Number Verification function

Three php form validation functions commonly used in project development

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