PHP access to search engine keywords source (support Baidu, Google and other search engines) function

Source: Internet
Author: User
  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 the 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 match 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 the no parameter domain name
  37. Preg_match (' @^ (?:/HTTP//)? ( [^/]+) @i ', $bul, $matches);
  38. $burl = $matches [1];
  39. Match Domain name settings
  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);//engine for 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);//engine for 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);//engine for 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);//engine for GBK
  74. $urlname = "Sogou:";
  75. $_session["urlname"]= $urlname;
  76. $_session["S_s_keyword"]= $s _s_keyword;
  77. }
  78. else if ($soso)
  79. {//From Search
  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);//engine for GBK
  83. $urlname = "Search:";
  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);//engine for 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);//engine for 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: In processing, remember to handle the problem of coding, because the different search engines, the results returned, some may be gbk, some may be the content of the UTF-8 format.

Function 2, this is also good, you can try. The code is as follows:

  1. Get portal keywords for search engines
  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, ' 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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.