网站地图    收藏   

主页 > php专栏 > php应用 >

php防止网站被攻击办法 - php高级应用

来源:自学PHP网    时间:2014-11-27 22:16 作者: 阅读:

[导读] 最近网站经常被攻击,后来想到了一个利用php来防止网站受攻击的办法,下面是我的代码,代码不是最好的,根据自己的需求来做,下面来看看我的代码 **网站防ip攻击代码(anti-ipattackcodewebsit...

php防止网站被攻击办法

最近网站经常被攻击,后来想到了一个利用php来防止网站受攻击的办法,下面是我的代码,代码不是最好的,根据自己的需求来做,下面来看看我的代码.

  1. /*  
  2. *网站防ip攻击代码(anti-ip attack code website)2010-11-20,ver2.0  
  3. *mydalle.com anti-refresh mechanism  
  4. *design by www.phpfensi.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. //开源代码phpfensi.com 
  59. }  
  60. }  
  61. }  
  62. if($yesno$str.=$checkip.$checkuri.$time."1rn";  
  63. file_put_contents($file,$str);  
  64. ?> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论