php取得搜尋引擎關鍵字來源(支援百度、Google等搜尋引擎)的函數

WBOY
發布: 2016-07-25 08:57:23
原創
1067 人瀏覽過
  1. //取得搜尋引擎入站關鍵字

  2. //by bbs.it-home.org
  3. function get_keyword($url,$kw_start)
  4. {
  5. $start=stripos($url,$kw_start);
  6. $url=substr($url,$start+strlen($kw_start) );
  7. $start=stripos($url,'&');
  8. if ($start>0)
  9. {
  10. $start=stripos($url,'&');
  11. $s_s_keyword=substr($url,0,$start);
  12. }
  13. else
  14. {
  15. $s_s_keyword=substr($url,0);
  16. }
  17. return $ s_s_keyword;
  18. }
  19. $url=isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';//取得入站url。

  20. $search_1="google.com"; //q= utf8
  21. $search_2="baidu.com"; //wd= gbk
  22. $search_3="yahoo.cn"; //q= utf8
  23. $search_4="sogou.com"; //query= gbk
  24. $search_5="soso.com"; //w= gbk
  25. $search_6="bing.com"; //q = utf8
  26. $search_7="youdao.com"; //q= utf8
  27. $google=preg_match("/b{$search_1}b/",$url);/ /記錄匹配情況,用於入站判斷。

  28. $baidu=preg_match("/b{$search_2}b/",$url);
  29. $yahoo=preg_match("/b{$search_3}b/",$url);
  30. $ sogou=preg_match("/b{$search_4}b/",$url);
  31. $soso=preg_match("/b{$search_5}b/",$url);
  32. $bing=preg_match( "/b{$search_6}b/",$url);
  33. $youdao=preg_match("/b{$search_7}b/",$url);
  34. $s_s_keyword="";
  35. $bul=$_SERVER['HTTP_REFERER'];
  36. //取得沒參數網域
  37. preg_match('@^(?:http://)?([^/]+)@i',$bul ,$matches);
  38. $burl=$matches[1];
  39. //匹配網域設定
  40. $curl="bbs.it-home.org";
  41. if($burl!= $curl){
  42. if ($google)
  43. {//來自google
  44. $s_s_keyword=get_keyword($url,'q=');//關鍵字前的字元為"q="。
  45. $s_s_keyword=urldecode($s_s_keyword);
  46. $urlname="Google:";
  47. $_SESSION["urlname"]=$urlname;
  48. $_SESSION["s_s_keyword"]=$s_s_keywordword ;
  49. //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎為gbk
  50. }
  51. else if($baidu)
  52. {//來自百度
  53. $s_s_keyword=get_keyword($url,'wd=');//關鍵字前的字元為"wd="。
  54. $s_s_keyword=urldecode($s_s_keyword);
  55. $s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎為gbk
  56. $urlname="百度:";
  57. $_SESSION["urlname"]=$urlname;
  58. $_SESSION["s_s_keyword"]=$s_s_keyword;
  59. }
  60. else if($yahoo)
  61. {//來自雅虎
  62. $s_s_keyword=get_keyword($url,'q=');//關鍵字前的字元為"q="。
  63. $s_s_keyword=urldecode($s_s_keyword);
  64. //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎為gbk
  65. $urlname="雅虎:雅虎:雅虎";
  66. $_SESSION["urlname"]=$urlname;
  67. $_SESSION["s_s_keyword"]=$s_s_keyword;
  68. }
  69. else if($sogou)
  70. {//來自搜狗
  71. $s_s_keyword=get_keyword($url,'query=');//關鍵字前的字元為"query="。
  72. $s_s_keyword=urldecode($s_s_keyword);
  73. $s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎為gbk
  74. $urlname="搜尋狗:";
  75. $_SESSION["urlname"]=$urlname;
  76. $_SESSION["s_s_keyword"]=$s_s_keyword;
  77. }
  78. else if($soso)
  79. {//來自搜尋
  80. $s_s_keyword=get_keyword($url,'w=');//關鍵字前的字元為"w="。
  81. $s_s_keyword=urldecode($s_s_keyword);
  82. $s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎為gbk
  83. $urlname="搜尋搜尋:" ;
  84. $_SESSION["urlname"]=$urlname;
  85. $_SESSION["s_s_keyword"]=$s_s_keyword;
  86. }
  87. else if($bing)
  88. {//來自必應
  89. $s_s_keyword=get_keyword($url,'q=');//關鍵字前的字元為"q="。
  90. $s_s_keyword=urldecode($s_s_keyword);
  91. //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎為gbk
  92. $urlname=" :";
  93. $_SESSION["urlname"]=$urlname;
  94. $_SESSION["s_s_keyword"]=$s_s_keyword;
  95. }
  96. else if($youdao)
  97. {///🎜>{//來自有道
  98. $s_s_keyword=get_keyword($url,'q=');//關鍵字前的字元為"q="。
  99. $s_s_keyword=urldecode($s_s_keyword);
  100. //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎為gbk
  101. $urlname="有道:";
  102. $_SESSION["urlname"]=$urlname;
  103. $_SESSION["s_s_keyword"]=$s_s_keyword;
  104. }
  105. else{
  106. $urlname=$burl; 🎜>$s_s_keyword="";
  107. $_SESSION["urlname"]=$urlname;
  108. $_SESSION["s_s_keyword"]=$s_s_keyword;
  109. }
  110. $s_urlname=$name; 🎜>$s_urlkey=$s_s_keyword;
  111. }
  112. else{
  113. $s_urlname=$_SESSION["urlname"];
  114. $s_urlkey=$_SESSION["s_s_key_key"]
  115. $s_urlkey=$_SESSION["s_s_key_key"]
  116. $s_urlkey=$_SESSION["s_s_key_key"]; 🎜>?>
  117. 複製程式碼

說明:在處理時,記得處理好編碼的問題,因為不同的搜尋引擎,回傳的結果,有的可​​能是GBK,有的則可能是UTF- 8格式的內容。

函數2,這個也不錯,大家可以試試看。 程式碼如下:

  1. //取得搜尋引擎的入口關鍵字
  2. $_SERVER['HTTP_REFERER']='http://www .baidu.com/s?wd=http://bbs.it-home.org';
  3. echo save_www_iiwnet_com_keyword('http://www.baidu.com/s?wd=http://bbs.it -home.org','http://www.baidu.com/s?wd=http://bbs.it-home.org');
  4. function save_www_iiwnet_com_keyword($domain,$path){
  5. if(strpos($domain, 'google.com.tw')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
  6. $ searchengine = 'GOOGLE TAIWAN';
  7. $keywords = urldecode($regs[1]); // google taiwan
  8. }
  9. if(strpos($domain,'google.cn')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
  10. $searchengine = 'GOOGLE CHINA';
  11. $keywords = urldecode($regs[1] ); // google china
  12. }
  13. if(strpos($domain,'google.com')!==false && preg_match('/q=([^&]*)/i',$path ,$regs)){
  14. $searchengine = 'GOOGLE';
  15. $keywords = urldecode($regs[1]); // google
  16. }elseif(strpos($domain,'baidu.') !==false && preg_match('/wd=([^&]*)/i',$path,$regs)){
  17. $searchengine = 'BAIDU';
  18. $keywords = urldecode($regs [1]); // baidu
  19. }elseif(strpos($domain,'baidu.')!==false && preg_match('/word=([^&]*)/i',$path,$ regs)){
  20. $searchengine = 'BAIDU';
  21. $keywords = urldecode($regs[1]); // baidu
  22. }elseif(strpos($domain,'114.vnet.cn' )!== false && preg_match('/kw=([^&]*)/i',$path,$regs)){
  23. $searchengine = 'CT114';
  24. $keywords = urldecode($ regs[1]); // ct114
  25. }elseif(strpos($domain,'iask.com')!==false && preg_match('/k=([^&]*)/i',$path ,$regs)){
  26. $searchengine = 'IASK';
  27. $keywords = urldecode($regs[1]); // iask
  28. }elseif(strpos($domain,'soso.com' )!==false && preg_match('/w=([^&]*)/i',$path,$regs)){
  29. $searchengine = 'SOSO';
  30. $keywords = urldecode($ regs[1]); // soso
  31. }elseif(strpos($domain, 'sogou.com')!==false && preg_match('/query=([^&]*)/i',$path ,$regs)){
  32. $searchengine = 'SOGOU';
  33. $keywords = urldecode($regs[1]); // sogou
  34. }elseif(strpos($domain,'so.163. com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
  35. $searchengine = 'NETEASE';
  36. $keywords = urldecode ($regs[1]); // netease
  37. }elseif(strpos($domain,'yodao.com')!== false && preg_match('/q=([^&]*)/i', $path,$regs)){
  38. $searchengine = 'YODAO';
  39. $keywords = urldecode($regs[1]); // yodao
  40. }elseif(strpos($domain,'zhongsou. com')!==false && preg_match('/word=([^&]*)/i',$path,$regs)){
  41. $searchengine = 'ZHONGSOU';
  42. $keywords = urldecode ($regs[1]); // zhongsou
  43. }elseif(strpos($domain,'search.tom.com')!==false && preg_match('/w=([^&]*)/i ',$path,$regs)){
  44. $searchengine = 'TOM';
  45. $keywords = urldecode($regs[1]); // tom
  46. }elseif(strpos($domain,' live.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
  47. $searchengine = 'MSLIVE';
  48. $keywords = urldecode($regs[1]); // MSLIVE
  49. }elseif(strpos($domain, 'tw.search.yahoo.com')!==false && preg_match('/p=([^&] *)/i',$path,$regs)){
  50. $searchengine = 'YAHOO TAIWAN';
  51. $keywords = urldecode($regs[1]); // yahoo taiwan
  52. }elseif( strpos($domain,'cn.yahoo.')!==false && preg_match('/p=([^&]*)/i',$path,$regs)){
  53. $searchengine = 'YAHOO CHINA';
  54. $keywords = urldecode($regs[1]); // yahoo china
  55. }elseif(strpos($domain,'yahoo.')!==false && preg_match('/p=( [^&]*)/i',$path,$regs)){
  56. $searchengine = 'YAHOO';
  57. $keywords = urldecode($regs[1]); // yahoo
  58. } elseif(strpos($domain,'msn.com.tw')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
  59. $searchengine = 'MSN TAIWAN';
  60. $keywords = urldecode($regs[1]); // msn taiwan
  61. }elseif(strpos($domain,'msn.com.cn')!==false && preg_match ('/q=([^&]*)/i',$path,$regs)){
  62. $searchengine = 'MSN CHINA';
  63. $keywords = urldecode($regs[1]); // msn china
  64. }elseif(strpos($domain,'msn.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)) {
  65. $searchengine = 'MSN';
  66. $keywords = urldecode($regs[1]); // msn
  67. }
  68. return $keywords;
  69. }
  70. ?>
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板