- 論壇徽章:
- 0
|
[PHP安全] 防止從外部提交數(shù)據的方法(非:HTTP_REFERER )
原帖由 "gzdkj" 發(fā)表:
1。防止別人把網頁抓到本地,修改表單控件后和各種參數(shù)后遠程提交數(shù)據;
2。防止那種自動發(fā)文的軟件在網站發(fā)布垃圾信息,就好像那種一次可以幾百個討論區(qū)發(fā)文的軟件;
郁悶,http_referer偽造一個就行了,不知你如何防止
- <?php
- $host = "www.123cha.com";
- $referer = "http://".$host;
- $fp = fsockopen ($host, 80, $errno, $errstr, 30);
- if (!$fp){
- echo "$errstr ($errno)<br>;\n";
- }else{
- $request = "
- GET / HTTP/1.1
- Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
- Referer: http://$host
- Accept-Language: zh-cn
- Accept-Encoding: gzip, deflate
- User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
- Host: $host
- Connection: Close"
- ."\r\n\r\n";
- fputs ($fp, "$request");
- while (!feof($fp))
- {
- $res[] = fgets($fp,1024);
- }
- $html = join("",$res);
- fclose ($fp);
- $fp = file_put_contents("123cha.html",$html);
- echo "done";
- }
復制代碼
這不就行了?
不過很奇怪的是,
www.hao123.com
的頁面抓下來是亂碼(除了http頭),這是為什么?難道是因為用了gzip之類壓縮?
- <?php
- $host = "www.hao123.com";
- $html = file_get_contents("http://".$host);
- $fp = file_put_contents("hao123.html",$html);
- echo "done";
- ?>;
復制代碼
但這樣抓的就沒問題.
再來分析開始抓的http頭
- HTTP/1.1 200 OK Date: Wed, 31 Aug 2005 00:59:36 GMT Server: Apache/1.3.27 Cache-Control: max-age=1296000 Expires: Thu, 15 Sep 2005 00:59:36 GMT Last-Modified: Mon, 29 Aug 2005 13:56:00 GMT Accept-Ranges: bytes Connection: close Content-Type: text/html Content-Encoding: gzip Content-Length: 14567
復制代碼
果然有這句,Content-Encoding: gzip
原來壓縮了的,長度14567字節(jié)了,
用第二種方法抓,原來沒壓縮的html是71143字節(jié),原來file_get_contents還可以自動解壓縮.
對這種不需要驗證http_referer和cookie之類的網頁,當然可以用第二種方法抓,但我想知道,如果用第一種方法抓,如何得到和第二種一樣的結果??? |
|