Website is being attacked php way to prevent it

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags address anti- code design file file_exists ip ip address

Recently the site is often attacked, and later thought of a way to use php tutorial to prevent the site from being attacked. Here is my code, the code is not the best, according to their needs to do, here's my code.
/ *
* Website ip attack code (anti-ip attack code website) 2010-11-20, ver2.0
* mydalle.com anti-refresh mechanism
* design by www.jzread.com
* /

<? php
// Query forbid ip
$ ip = $ _ server ['remote_addr'];
$ fileht = ". htaccess2";
if (! file_exists ($ fileht)) file_put_contents ($ fileht, "");
$ filehtarr = @ file ($ fileht);
if (in_array ($ ip. "rn", $ filehtarr)) die ("warning:". "<br>". "your ip address are forbided by mydalle.com anti-refresh mechanism, if you have any question pls emill to shop@mydalle.com! <br> (mydalle.com anti-refresh mechanism is to enable users to have a good shipping services, but there maybe some inevitable network problems in your ip address, so that you can mail to us to solve ) ");

// join ban ip
$ time = time ();
$ fileforbid = "log / forbidchk.dat";
if (file_exists ($ fileforbid))
{if ($ time-filemtime ($ fileforbid)> 30) unlink ($ fileforbid);
else {
$ fileforbidarr = @ file ($ fileforbid);
if ($ ip == substr ($ fileforbidarr [0], 0, strlen ($ ip)))
{
if ($ time-substr ($ fileforbidarr [1], 0, strlen ($ time))> 120) unlink ($ fileforbid);
elseif ($ fileforbidarr [2]> 120) {file_put_contents ($ fileht, $ ip. "rn", file_append); unlink ($ fileforbid);}
else {$ fileforbidarr [2] ++; file_put_contents ($ fileforbid, $ fileforbidarr);}
}
}
}
// anti-refresh
$ str = "";
$ file = "log / ipdate.dat";
if (! file_exists ("log") &&! is_dir ("log")) mkdir ("log", 0777);
if (! file_exists ($ file)) file_put_contents ($ file, "");
$ allowtime = 60; / / anti-refresh time
$ allownum = 5; / / anti-refresh the number
$ uri = $ _ server ['request_uri'];
$ checkip = md5 ($ ip);
$ checkuri = md5 ($ uri);
$ yesno = true;
$ ipdate = @ file ($ file);
foreach ($ ipdate as $ k => $ v)
{$ iptem = substr ($ v, 0,32);
$ uritem = substr ($ v, 32,32);
$ timetem = substr ($ v, 64,10);
$ numtem = substr ($ v, 74);
if ($ time- $ timetem <$ allowtime) {
if ($ iptem! = $ checkip) $ str. = $ v;
else {
$ yesno = false;
if ($ uritem! = $ checkuri) $ str. = $ iptem. $ checkuri. $ time. "1rn";
elseif ($ numtem <$ allownum) $ str. = $ iptem. $ uritem. $ timetem. ($ numtem + 1). "rn";
else
{
if (! file_exists ($ fileforbid)) {$ addforbidarr = array ($ ip. "rn", time (). "rn", 1); file_put_contents ($ fileforbid, $ addforbidarr);}
file_put_contents ("log / forbided_ip.log", $ ip. "-". date ("ymd h: i: s", time ()). "-". $ uri. "rn", file_append);
$ timepass = $ timetem + $ allowtime- $ time;
die ("warning:". "<br>". "pls do not refresh too frequently, and wait for". $ timepass. "seconds to continue, if not your ip address will be forbided automatic by mydalle.com anti- refresh mechanism! <br> (mydalle.com anti-refresh mechanism is to enable users to have a good shipping services, but there maybe some inevitable network problems in your ip address, so that you can mail to us to solve.) ") ;
}
}
}
}
if ($ yesno) $ str. = $ checkip. $ checkuri. $ time. "1rn";
file_put_contents ($ file, $ str);
?>

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.