php採集模擬點擊僞造IP,僞造瀏覽器useragent僞造來源防反爬蟲例子

先上兩個文件 index.php  一個curl.php

index.php是我們要採集的。以下是源碼

[html] view plain copy
  1. $client_ip = getip();  
  2. $referer = getreferer();  
  3.   
  4. $allow_ip = '192.168.1.100';  
  5. $allow_referer = 'http://www.amztool.cn';  
  6. $useragent=$_SERVER['HTTP_USER_AGENT'];  
  7. plog( 'client_ip='.$client_ip." || useragent=".$useragent.' ');  
  8. echo '<br>client_ip=';  
  9. echo $client_ip;  
  10. echo " || useragent=";  
  11. echo $useragent;  
  12.   
  13. echo '<hr><br>';  
  14. // 獲取訪問者ip  
  15. function getip(){  
  16.     if(!empty($_SERVER['HTTP_CLIENT_IP'])){  
  17.         $cip = $_SERVER['HTTP_CLIENT_IP'];  
  18.     }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){  
  19.         $cip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
  20.     }elseif(!empty($_SERVER['REMOTE_ADDR'])){  
  21.         $cip = $_SERVER['REMOTE_ADDR'];  
  22.     }else{  
  23.         $cip = '';  
  24.     }  
  25.     return $cip;  
  26. }  
  27.   
  28. // 獲取訪問者來源  
  29. function getreferer(){  
  30.     if(isset($_SERVER['HTTP_REFERER'])){  
  31.         return $_SERVER['HTTP_REFERER'];  
  32.     }  
  33.     return '';  
  34. }  
  35.   
  36. function plog($message){  
  37.     $file=__DIR__."/text.txt";  
  38.   
  39.      if($f  = file_put_contents($file, $message."\r\n",FILE_APPEND)){// 這個函數支持版本(PHP 5) 打印到文件  
  40.   
  41.         }  
  42. }  

再上curl.php 這個文件是用來採集的執行文件

[html] view plain copy
  1. $ch = curl_init();  
  2. $curlurl = "http://www.amztool.cn/index.php";  
  3. $referurl = "http://www.amztool.cn";  
  4. $ip=mt_rand(11, 191).".".mt_rand(0, 240).".".mt_rand(1, 240).".".mt_rand(1, 240);   //隨機ip  
  5. $agentarry=[  
  6.     //PC端的UserAgent  
  7.     "safari 5.1 – MAC"=>"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",  
  8.     "safari 5.1 – Windows"=>"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",  
  9.     "Firefox 38esr"=>"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",  
  10.     "IE 11"=>"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",  
  11.     "IE 9.0"=>"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0",  
  12.     "IE 8.0"=>"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",  
  13.     "IE 7.0"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",  
  14.     "IE 6.0"=>"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",  
  15.     "Firefox 4.0.1 – MAC"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",  
  16.     "Firefox 4.0.1 – Windows"=>"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",  
  17.     "Opera 11.11 – MAC"=>"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",  
  18.     "Opera 11.11 – Windows"=>"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",  
  19.     "Chrome 17.0 – MAC"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",  
  20.     "傲遊(Maxthon)"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",  
  21.     "騰訊TT"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",  
  22.     "世界之窗(The World) 2.x"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",  
  23.     "世界之窗(The World) 3.x"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",  
  24.     "360瀏覽器"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",  
  25.     "搜狗瀏覽器 1.x"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",  
  26.     "Avant"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",  
  27.     "Green Browser"=>"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",  
  28.     //移動端口  
  29.     "safari iOS 4.33 – iPhone"=>"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",  
  30.     "safari iOS 4.33 – iPod Touch"=>"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",  
  31.     "safari iOS 4.33 – iPad"=>"Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",  
  32.     "Android N1"=>"Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",  
  33.     "Android QQ瀏覽器 For android"=>"MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",  
  34.     "Android Opera Mobile"=>"Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",  
  35.     "Android Pad Moto Xoom"=>"Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",  
  36.     "BlackBerry"=>"Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",  
  37.     "WebOS HP Touchpad"=>"Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",  
  38.     "UC標準"=>"NOKIA5700/ UCWEB7.0.2.37/28/999",  
  39.     "UCOpenwave"=>"Openwave/ UCWEB7.0.2.37/28/999",  
  40.     "UC Opera"=>"Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999",  
  41.     "微信內置瀏覽器"=>"Mozilla/5.0 (Linux; Android 6.0; 1503-M02 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.2 TBS/036558 Safari/537.36 MicroMessenger/6.3.25.861 NetType/WIFI Language/zh_CN",  
  42.    // ""=>"",  
  43.   
  44. ];  
  45. //$useragent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";  //要得到類似這樣useranget 可以自定義  
  46. $useragent=$agentarry[array_rand($agentarry,1)];  //隨機瀏覽器useragent  
  47. $header = array(  
  48.     'CLIENT-IP:'.$ip,  
  49.     'X-FORWARDED-FOR:'.$ip,  
  50. );    //構造ip  
  51. curl_setopt($ch, CURLOPT_URL, $curlurl); //要抓取的網址  
  52. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);  
  54. curl_setopt($ch, CURLOPT_REFERER, $referurl);  //模擬來源網址  
  55. curl_setopt($ch, CURLOPT_USERAGENT, $useragent); //模擬常用瀏覽器的useragent  
  56.   
  57. $page_content = curl_exec($ch);  
  58. curl_close($ch);  
  59. echo $page_content;  

這兩個文件放在同一目錄,目錄下新建一個text.txt文件用來記錄採集的仿問的ip和useragent頭。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章