国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php防止網站被攻擊辦法

2024-05-04 21:48:20
字體:
來源:轉載
供稿:網友

最近網站經常被攻擊,后來想到了一個利用php來防止網站受攻擊的辦法,下面是我的代碼,代碼不是最好的,根據自己的需求來做,下面來看看我的代碼.

  1. /*  
  2. *網站防ip攻擊代碼(anti-ip attack code website)2010-11-20,ver2.0  
  3. *mydalle.com anti-refresh mechanism  
  4. *design by m.survivalescaperooms.com 
  5. */ 
  6.  
  7. //查詢禁止ip  
  8. $ip =$_server['remote_addr'];  
  9. $fileht=".htaccess2";  
  10. if(!file_exists($fileht))file_put_contents($fileht,"");  
  11. $filehtarr=@file($fileht);  
  12. if(in_array($ip."rn",$filehtarr))die("warning:"."
    "
    ."your ip address are forbided by mydalle.com anti-refresh mechanism, if you have any question pls emill to shop@mydalle.com!
    (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.)"
    ); 
  13.  
  14. //加入禁止ip  
  15. $time=time();  
  16. $fileforbid="log/forbidchk.dat";  
  17. if(file_exists($fileforbid))  
  18. if($time-filemtime($fileforbid)>30)unlink($fileforbid);  
  19. else{  
  20. $fileforbidarr=@file($fileforbid);  
  21. if($ip==substr($fileforbidarr[0],0,strlen($ip)))  
  22. {  
  23. if($time-substr($fileforbidarr[1],0,strlen($time))>120)unlink($fileforbid);  
  24. elseif($fileforbidarr[2]>120){file_put_contents($fileht,$ip."rn",file_append);unlink($fileforbid);}  
  25. else{$fileforbidarr[2]++;file_put_contents($fileforbid,$fileforbidarr);}  
  26. }  
  27. }  
  28. }  
  29. //防刷新  
  30. $str="";  
  31. $file="log/ipdate.dat";  
  32. if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);  
  33. if(!file_exists($file))file_put_contents($file,"");  
  34. $allowtime = 60;//防刷新時間  
  35. $allownum=5;//防刷新次數  
  36. $uri=$_server['request_uri'];  
  37. $checkip=md5($ip);  
  38. $checkuri=md5($uri);  
  39. $yesno=true;  
  40. $ipdate=@file($file);  
  41. foreach($ipdate as $k=>$v)  
  42. $iptem=substr($v,0,32);  
  43. $uritem=substr($v,32,32);  
  44. $timetem=substr($v,64,10);  
  45. $numtem=substr($v,74);  
  46. if($time-$timetem<$allowtime){  
  47. if($iptem!=$checkip)$str.=$v;  
  48. else{  
  49. $yesno=false;  
  50. if($uritem!=$checkuri)$str.=$iptem.$checkuri.$time."1rn";  
  51. elseif($numtem<$allownum)$str.=$iptem.$uritem.$timetem.($numtem+1)."rn";  
  52. else  
  53. {  
  54. if(!file_exists($fileforbid)){$addforbidarr=array($ip."rn",time()."rn",1);file_put_contents($fileforbid,$addforbidarr);}  
  55. file_put_contents("log/forbided_ip.log",$ip."--".date("y-m-d h:i:s",time())."--".$uri."rn",file_append);  
  56. $timepass=$timetem+$allowtime-$time;  
  57. die("warning:"."
    "
    ."pls don't 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!
    (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.)"
    );  
  58. //開源代碼Vevb.com 
  59. }  
  60. }  
  61. }  
  62. if($yesno$str.=$checkip.$checkuri.$time."1rn";  
  63. file_put_contents($file,$str);  
  64. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定边县| 菏泽市| 茂名市| 通化市| 泸定县| 曲水县| 米林县| 临安市| 荥经县| 布拖县| 桑植县| 富顺县| 吉木萨尔县| 定兴县| 织金县| 宁安市| 朝阳县| 宝清县| 河池市| 特克斯县| 舟曲县| 阜南县| 习水县| 新龙县| 盈江县| 亚东县| 喀什市| 临夏市| 宝兴县| 霍邱县| 泸溪县| 册亨县| 大埔县| 阿拉善右旗| 汉阴县| 绥芬河市| 新津县| 雷波县| 逊克县| 吉林省| 西吉县|