Php implements the IP address blocking function [original] _ PHP Tutorial

Source: Internet
Author: User
Php implements the IP address blocking function [original]. 2008-01-2400: 00: 00 this article by the site Original: www.drise.cn author: www.drise.cn reprint Please Note: from www. drise. cnqq: 271728967 when I got up early, I suddenly saw someone on my website in a bad situation. 00:00:00

//

This article was originally written on this site: www.drise.cn

Author: www.drise.cn

Reprinted Please Note: from www.drise.cn

Qq: 271728967

//

When I got up early, I suddenly saw that someone on my website was messing up. to prevent such a problem, I thought of a way to block the user's IP address.

On the homepage, we create a database:

Create table 'Su _ lockip '(
'Id' int (4) not null auto_increment,
'Lockip' varchar (1024) default NULL,
Primary key ('id ')
)

Create a page for a file on the next page, mainly because the user writes IP addresses separated by "|". This page will not be written much, so I will simply write it, code for warehouse receiving $ UlockIp =$ _ POST ['Z']? $ _ POST ['Z']: '';
If (empty ($ UlockIp )){
Exit ("script" alert ('sorry, the information you entered is incorrect! '); History. back (); script ");
}
$ SQL = "update su_lockip set lockip = '$ ulockip '";
If (mysql_query ($ SQL )){
Exit ("script" alert ('locked successfully! '); History. back (); script ");
} Else {
Exit ("script" alert ('sorry, the information you entered is incorrect! '); History. back (); script ");
}

In this case, the user is locked. the following code is based on whether the user IP address is stored in the data. if the user IP address is found, the system is killed. the code is as follows:

Function lock_user_ip (){
$ Usql = mysql_query ("select * from su_lockip ");
$ Urs = mysql_fetch_array ($ Usql );
$ UlockIp = $ Urs ['lockip'];
$ ClockIp = $ this-> get_real_ip ();
$ Iplist = explode ('|', $ UlockIp );
If (in_array ($ ClockIp, $ Iplist )){
Exit ('Sorry system lock your IP ');
}
}

Function get_real_ip () {// this code comes from the Internet.
$ 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];
Break;
}
}
}
Return ($ ip? $ Ip: $ _ SERVER ['remote _ ADDR ']);
}

Haha, it's so easy to write, and no security filtering is performed.

Http://www.bkjia.com/PHPjc/631849.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631849.htmlTechArticle2008-01-24 00:00:00 // this article by the site Original: www.drise.cn author: www.drise.cn reprint Please Note: from www.drise.cn qq: 271728967 // get up early suddenly saw, my website was in nausea and chaos...

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.