Php implements the IP address blocking function [original]

Source: Internet
Author: User

2008-01-24 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.

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.