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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

See all articles