PHP creates an instance code for abnormal logon ip address detection, and an ip address instance

Source: Internet
Author: User

PHP creates an instance code for abnormal logon ip address detection, and an ip address instance

Implement database traversal through function Query

/*** Do not return the red information when logging on to a common ip Address * @ param string $ name username * @ return string */function errorIp ($ ip, $ name) {$ nowip = get_client_ip (); // checks whether the ip address and the current ip address are the same. if ($ ip = $ nowip) {// returns the same string $ str = '<font color = "blue "'. "> logon IP Address :". $ ip. "</font>";} else {// The number of IP addresses in different records $ count = M ('log ') -> where ("name = '{$ name}' AND ip = '{$ ip}'")-> count (); // if a certain number of ip addresses are exceeded, a normal ip address is returned. Otherwise, an exception is returned. if ($ count> 10) {$ str = '<font color = "blue "'. "> logon IP Address :". $ ip. "</font>";} else {$ str = '<font color = "red "'. "> abnormal IP Address :". $ ip. "</font>" ;}} return $ str ;}

Note: This function is applicable to all frameworks. get_client_ip () is a function for obtaining ip addresses.

Get_client_ip function segment:

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(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {    $ip = getenv('REMOTE_ADDR');  } else{    $ip = $_SERVER['REMOTE_ADDR'];  }  return $ip;}

The above example code of the PHP login abnormal ip detection function is all the content that I have shared with you. I hope to give you a reference, and I hope you can provide more support for the house of friends.

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.