PHP function to obtain search engine keyword sources (supports search engines such as Baidu and Google)

WBOY
Release: 2016-07-25 08:57:23
Original
1047 people have browsed it
  1. //Get inbound keywords from search engines

  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']:'';//Get inbound 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);//Record the matching situation for inbound judgment.

  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. //Get it? Parameter domain name
  37. preg_match('@^(?:http://)?([^/]+)@i',$bul,$matches);
  38. $burl=$matches[1];
  39. //Matching domain name Set
  40. $curl="bbs.it-home.org";
  41. if($burl!=$curl){
  42. if ($google)
  43. {//from google
  44. $s_s_keyword=get_keyword($url,'q= ');//The character before the keyword is "q=".
  45. $s_s_keyword=urldecode($s_s_keyword);
  46. $urlname="Google:";
  47. $_SESSION["urlname"]=$urlname;
  48. $_SESSION["s_s_keyword"]=$s_s_keyword;
  49. //$s_s_keyword=iconv ("GBK","UTF-8",$s_s_keyword);//The engine is gbk
  50. }
  51. else if($baidu)
  52. {//From Baidu
  53. $s_s_keyword=get_keyword($url,'wd='); //The character before the keyword is "wd=".
  54. $s_s_keyword=urldecode($s_s_keyword);
  55. $s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//The engine is gbk
  56. $urlname="Baidu:";
  57. $_SESSION["urlname "]=$urlname;
  58. $_SESSION["s_s_keyword"]=$s_s_keyword;
  59. }
  60. else if($yahoo)
  61. {//From Yahoo
  62. $s_s_keyword=get_keyword($url,'q=');// The character before the keyword is "q=".
  63. $s_s_keyword=urldecode($s_s_keyword);
  64. //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//The engine is gbk
  65. $urlname="Yahoo:";
  66. $_SESSION[ "urlname"]=$urlname;
  67. $_SESSION["s_s_keyword"]=$s_s_keyword;
  68. }
  69. else if($sogou)
  70. {//From Sogou
  71. $s_s_keyword=get_keyword($url,'query='); //The character before the keyword is "query=".
  72. $s_s_keyword=urldecode($s_s_keyword);
  73. $s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//The engine is gbk
  74. $urlname="Sogou:";
  75. $_SESSION["urlname "]=$urlname;
  76. $_SESSION["s_s_keyword"]=$s_s_keyword;
  77. }
  78. else if($soso)
  79. {//From Soso
  80. $s_s_keyword=get_keyword($url,'w=');/ /The character before the keyword is "w=".
  81. $s_s_keyword=urldecode($s_s_keyword);
  82. $s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//The engine is gbk
  83. $urlname="Soso:";
  84. $_SESSION[" urlname"]=$urlname;
  85. $_SESSION["s_s_keyword"]=$s_s_keyword;
  86. }
  87. else if($bing)
  88. {//From Bing
  89. $s_s_keyword=get_keyword($url,'q='); //The character before the keyword is "q=".
  90. $s_s_keyword=urldecode($s_s_keyword);
  91. //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//The engine is gbk
  92. $urlname="Bing:";
  93. $_SESSION ["urlname"]=$urlname;
  94. $_SESSION["s_s_keyword"]=$s_s_keyword;
  95. }
  96. else if($youdao)
  97. {//From Youdao
  98. $s_s_keyword=get_keyword($url,'q=' );//The character before the keyword is "q=".
  99. $s_s_keyword=urldecode($s_s_keyword);
  100. //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//The engine is gbk
  101. $urlname="Youdao:";
  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. ?>
Copy code

Note: When processing, remember to deal with the encoding issue, because different search engines may return results in GBK, while others may be in UTF-8 format.

Function 2, this is also good, you can try it. code show as below:

  1. //Get the entry keyword of the search engine
  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. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!