Functions of php to obtain the source of search engine keywords (support for Baidu, Google, and other search engines)

Source: Internet
Author: User
Functions of php to obtain the source of search engine keywords (support for Baidu, Google, and other search engines)

  1. // Obtain the keyword from the search engine inbound

  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 matching information 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. // Retrieve domain names without parameters
  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); // 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 sousearch
  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 = "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); // 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. ?>

(In the processing, remember to deal with the encoding problem, because of different search engines, returned results, some may be GBK, some may be the content of the UTF-8 format.

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

  1. // Obtain the keyword of the search engine entry
  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, '2017 .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 = 'deletease ';
  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. ?>

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.