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

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

Jul 25, 2016 am 08:57 AM

  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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles