《CMS系統:phpcms v9添加百度自動推送JS代碼并解決重復推送問題》要點:
本文介紹了CMS系統:phpcms v9添加百度自動推送JS代碼并解決重復推送問題,希望對您有用。如果有疑問,可以聯系我們。
相關主題:PHPCMS教程
- function?baidu_check_record($url){???????
- ?????????if(strpos($url,'.html')){?
- ??????????$baidu_cache?=?getcache('baidurecord','baidu');?
- ??????????$baidu_cache=unserialize($baidu_cache);?
- ??????????$pathurl=str_replace(APP_PATH,'',$url);?
- ??????????$array=explode('-',$pathurl);?
- ???????????if($array[1]?&&?$array[2]?&&?$baidu_cache[$array[1].'-'.$array[2]]?==?1){?
- ???????????????return?1;?
- ???????????}else{?
- ?????????????????$url='http://www.baidu.com/s?wd='.$url;?
- ????????????????$curl=curl_init();?
- ????????????????curl_setopt($curl,CURLOPT_URL,$url);?
- ????????????????curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);?
- ????????????????curl_setopt($curl,?CURLOPT_ENCODING,?'gzip');?
- ????????????????$rsurl=curl_exec($curl);?
- ????????????????curl_close($curl);?
- ????????????????//如果抓取到的百度結果中不存在【提交網址】這個關鍵詞,則認為該頁面已被百度收錄?
- ????????????????if(!preg_match_all('/提交網址/i',$rsurl,$match1)?&&?preg_match_all('/百度為您找到相關結果/i',$rsurl,$match2)){?
- ???????????????????$baidu_cache[$array[1].'-'.$array[2]]=1;?
- ???????????????????$baidu_cache=serialize($baidu_cache);?
- ????????????????????setcache('baidurecord',$baidu_cache,'baidu');?
- ????????????????????return?1;?
- ????????????????}?else?{?
- ????????????????????return?0;?
- ????????????????}?
- ??????????????????
- ???????????}?
- ????????????
- ??????????
- ?????????}?
- ???????
- }?
- ?
- function?bdPushData($catid,$id)?{?
- ????if?($_SERVER['HTTPS']?!=?"on")?{?
- ????????$currentUrl?=?'http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];?
- ????}?else?{?
- ????????$currentUrl?=?'https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];?
- ????}?
- ????if(!$catid?||?!$id)?return;?
- ????$get_url=go($catid,$id);?
- ????//判斷是否是未收錄頁面,并且當前頁面是否等于正規url地址(get_premalink)?
- ????if(baidu_check_record($get_url)?==?0?&&?$currentUrl?==?$get_url)?{?
- ????????echo?"<script>(function(){?
- ????????????var?bp?=?document.createElement('script');?
- ????????????var?curProtocol?=?window.location.protocol.split(':')[0];?
- ????????????if?(curProtocol?===?'https')?{?
- ????????????????bp.src?=?'https://zz.bdstatic.com/linksubmit/push.js';?????????
- ????????????}?else?{?
- ????????????????bp.src?=?'http://push.zhanzhang.baidu.com/push.js';?
- ????????????}?
- ????????????var?s?=?document.getElementsByTagName('script')[0];?
- ????????????s.parentNode.insertBefore(bp,?s);?
- ????????????})();</script>";?
- ??????}?
- ???}?
在頁面底部加上如下代碼:
?CMS技巧
工作原理是文章加載時,會再百度搜索當前文章的url地址,如果百度未收錄,查詢結果中會匹配到“沒有找到該URl.您可以直接訪問”或"很抱歉,沒有找到與"文字內容.當代碼確認頁面已經收錄時,將會把文章的欄目catid和文章id保存成數組,存儲到緩存,方便下次直接獲取來判斷是否已經推送過了.
百度自動推送服務對象及原理
JS鏈接推送代碼以網頁為最小對象,服務于全平臺多終端,PC站和移動站均可使用.
安裝代碼的頁面在任意平臺(瀏覽器、微信、微博)被加載時,頁面鏈接會被第一時間推送給百度,從而提高站點新內容的發現速度.CMS技巧
CMS技巧
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/5761.html