PHP和JS判断来路跳转到指定页面
PHP根据referer跳转,代码如下:
<?php
$ref = $_SERVER['HTTP_REFERER'];
if (stripos($ref, "baidu") || stripos($ref, "google") {
header("Location: http://www.phprm.com");
exit;
}
?>根据UA跳转,代码如下:
<?php
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if (stripos($userAgent, "Moz") || stripos($userAgent, "baidu")) {
header("Location: http://www.phprm.com");
}
?>header()函数的定义如下:
void header (string string [,bool replace [,int http_response_code]])
可选参数replace指明是替换前一条类似标头还是添加一条相同类型的标头,默认为替换。
第二个可选参数http_response_code强制将HTTP相应代码设为指定值。 header函数中Location类型的标头是一种特殊的header调用,常用来实现页面跳转。注意:1.location和“:”号间不能有空格,否则不会跳转。
JS判断方法,代码如下:
<script>
var s = document.referrer;
if (s.indexOf("baidu") > 0 || s.indexOf("soso") > 0 || s.indexOf("google") > 0 || s.indexOf("yahoo") > 0 || s.indexOf("sogou") > 0 || s.indexOf("youdao") > 0 || s.indexOf("bing") > 0) {
self.location = "http://www.phprm.com";
}
</script>
本文地址:http://www.phprm.com/develop/fs1575.html
转载随意,但请附上文章地址:-)