Maison  >  Article  >  développement back-end  >  php获取搜索引擎关键字来源(支持百度、谷歌等搜索引擎)的函数

php获取搜索引擎关键字来源(支持百度、谷歌等搜索引擎)的函数

WBOY
WBOYoriginal
2016-07-25 08:57:231052parcourir
  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="谷歌:";
  47. $_SESSION["urlname"]=$urlname;
  48. $_SESSION["s_s_keyword"]=$s_s_keyword;
  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;
  107. $s_s_keyword="";
  108. $_SESSION["urlname"]=$urlname;
  109. $_SESSION["s_s_keyword"]=$s_s_keyword;
  110. }
  111. $s_urlname=$urlname;
  112. $s_urlkey=$s_s_keyword;
  113. }
  114. else{
  115. $s_urlname=$_SESSION["urlname"];
  116. $s_urlkey=$_SESSION["s_s_keyword"];
  117. }
  118. ?>
复制代码

说明:在处理时,记得处理好编码的问题,因为不同的搜索引擎,返回的结果,有的可能是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. ?>
复制代码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn