PHP to make login exception IP detection function instance code _php instance

Source: Internet
Author: User

Using function Query Database traversal implementation

/**
 * Does not log on to the usual IP address alphabet information
 * @param string $ip  IP Address
 * @param string $name  username
 * @return string
 * *

function Errorip ($ip, $name) {

  $nowip = Get_client_ip ();
  Determine whether the IP and current IP are the same, different query database compare
  if ($ip = = $nowip) {
    //same direct return string
    $str = ' <font color= ' blue '. ' > Login IP: ". $ip." </font> ";
  } else{
    //The number of this IP address is
    $count =  M (' log ')->where ("Name= ' {$name} ' and ip= ' {$ip} '")->count ();
    If more than a certain number is normal IP otherwise the exception returns the string
    if ($count >) {
      $str = ' <font color= ' Blue '. > Login IP: ". $ip." </font> ";
    } else{
      $str = ' <font color= ' red '. ' > Abnormal IP: ". $ip." </font> ";
    }
  }

  return $str;

}

Note: For all frameworks, Get_client_ip () is an IP capture function.

Get_client_ip function Fragment:

function Get_client_ip () {
  if (getenv (' http_client_ip ') && strcasecmp (getenv (' http_client_ip '), ' Unknown ')) {
    $ip = getenv (' http_client_ip ');
  } elseif (getenv (' http_x_forwarded_for ') && strcasecmp ( getenv (' http_x_forwarded_for '), ' unknown ') {
    $ip = getenv (' http_x_forwarded_for '); ElseIf ('
  REMOTE _addr ') && strcasecmp (getenv (' remote_addr '), ' unknown ') {
    $ip = getenv (' remote_addr ');
  } else{
    $ ip = $_server[' remote_addr '];
  }
  return $ip;
}

Above this PHP to make login abnormal IP detection function of the example code is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.