登录  /  注册
首页 > php教程 > php手册 > 正文
php HTTP_REFERER函数的使用用法
php中文网
发布: 2016-06-13 11:18:59
原创
1766人浏览过

利用php教程的http_referer函数来判断用户的来路,这是简单了,
实例

if (isset($_SERVER['HTTP_REFERER'])) {
print "The page you were on previously was {$_SERVER['HTTP_REFERER']}
";
} else {
print "You didn't click any links to get here
";
}
?>

Click me!

下面我们让用户不知道我们的来路处理
实例

]$host = "www.123cha.com";
$referer = "http://".$host;
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr ($errno)
;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"
."rnrn";


fputs ($fp, "$request");
while (!feof($fp))
{
$res[] = fgets($fp,1024);
}
$html = join("",$res);
fclose ($fp);
$fp = file_put_contents("123cha.html",$html);
echo "done";
}[/code]

这不就行了?

不过很奇怪的是,
www.hao123.com
的页面抓下来是乱码(除了http头),这是为什么?难道是因为用了gzip之类压缩?

[code]$host = "www.zhutiai.com";
$html = file_get_contents("http://".$host);
$fp = file_put_contents("hao123.html",$html);
echo "done";
?>;[/code]


但这样抓的就没问题.

再来分析开始抓的http头
[code]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[/code]

果然有这句,Content-Encoding: gzip
原来压缩了的,长度14567字节了,
用第二种方法抓,原来没压缩的html是71143字节,原来file_get_contents还可以自动解压缩.


实例二

$host = '127.0.0.1';
$target = '/2.php';
$referer = 'http://www.bkjia.com'; //伪造HTTP_REFERER地址
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr($errno)
n";
}
else{
$out = "
GET $target HTTP/1.1
Host: $host
Referer: $referer
Connection: Closernrn";
fwrite($fp, $out);
while (!feof($fp)){
echo fgets($fp, 1024);
}
fclose($fp);
}
?>

另一个2.php文件很简单,只是写上一行读取当前的HTTP_REFERER服务器值的代码即可,如下:

echo "


";
echo $_SERVER["HTTP_REFERER"];
?>


来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学