PHP和JS判斷來(lái)路跳轉(zhuǎn)到指定頁(yè)面
來(lái)源:程序員人生 發(fā)布時(shí)間:2013-11-28 01:34:57 閱讀次數(shù):3221次
PHP根據(jù)referer跳轉(zhuǎn),代碼如下:
- <?php
- $ref = $_SERVER['HTTP_REFERER'];
- if(stripos($ref,"baidu") || stripos($ref,"google")
- {
- header("Location: http://www.phpfensi.com");
- exit;
- }
- ?>
根據(jù)UA跳轉(zhuǎn),代碼如下:
- <?php
- $userAgent = $_SERVER['HTTP_USER_AGENT'];
- if(stripos($userAgent,"Moz") || stripos($userAgent,"baidu"))
- {
- header("Location: http://www.phpfensi.com");
- }
- ?>
header()函數(shù)的定義如下:
void header (string string [,bool replace [,int http_response_code]])
可選參數(shù)replace指明是替換前一條類(lèi)似標(biāo)頭還是添加一條相同類(lèi)型的標(biāo)頭,默認(rèn)為替換。
第二個(gè)可選參數(shù)http_response_code強(qiáng)制將HTTP相應(yīng)代碼設(shè)為指定值。 header函數(shù)中Location類(lèi)型的標(biāo)頭是一種特殊的header調(diào)用,常用來(lái)實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)。注意:1.location和“:”號(hào)間不能有空格,否則不會(huì)跳轉(zhuǎn)。
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.phpfensi.com";
- }
- </script>
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)