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.