加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP和JS判断来路跳转到指定页面

发布时间:2020-12-13 20:31:12 所属栏目:PHP教程 来源:网络整理
导读:PHP根据referer跳转, 代码如下: ?php $ref = $_SERVER [ HTTP_REFERER ]; if ( stripos ( $ref , baidu )|| stripos ( $ref , google ) { header( Location:http://www.phpfensi.com ); exit ; } ? 根据UA跳转, 代码如下: ?php $userAgent = $_SERVER [ HTTP

PHP根据referer跳转,代码如下:

  1. <?php 
  2. $ref = $_SERVER['HTTP_REFERER']; 
  3.         if(stripos($ref,"baidu") || stripos($ref,"google"
  4.  { 
  5.   header("Location: http://www.phpfensi.com");  
  6.   exit
  7.  } 
  8. ?> 

根据UA跳转,代码如下:

  1. <?php 
  2. $userAgent = $_SERVER['HTTP_USER_AGENT']; 
  3. if(stripos($userAgent,"Moz") || stripos($userAgent,"baidu")) 
  4.     { 
  5.         header("Location: http://www.phpfensi.com"); 
  6.     } 
  7. ?> 

header()函数的定义如下:

void header (string string [,bool replace [,int http_response_code]])

可选参数replace指明是替换前一条类似标头还是添加一条相同类型的标头,默认为替换。

第二个可选参数http_response_code强制将HTTP相应代码设为指定值。 header函数中Location类型的标头是一种特殊的header调用,常用来实现页面跳转。注意:1.location和“:”号间不能有空格,否则不会跳转。

JS判断方法,代码如下:

  1. <script> 
  2.  var s=document.referrer; 
  3.  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) 
  4.  { 
  5.  self.location="http://www.phpfensi.com"
  6.  } 
  7. </script> 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读