PHP functions for retrieving search engine keyword sources (support for search engines such as Baidu and Google)

Source: Internet
Author: User
Add a field through the website function, get the source keyword, how to get the source keyword, the code is sent below, including (Baidu, Google, Yahoo, Sogou, search, Bing, Youdao) the code shows how to obtain several search engines. I hope it will be helpful to you. I 'd like to share with you that a customer needs a feature in a recent project, to place an order for a product, you need to know the path from which the user places the order. the customer service software (53 customer service) and webmaster statistics can only meet the requirements of working hours, and the rest of work in the evening and on Saturday, the customer service is not online, and the user does not know the specific source of the order. Therefore, you can only add a field through the website function to obtain the source keyword and how to obtain the source keyword, the code is published below, which contains several methods for obtaining search engines (Baidu, Google, Yahoo, Sogou, sousearch, Bing, and Youdao). the code indicates that it is helpful to you, share

The code is as follows:
The code is as follows:
// Obtain keywords from the search engine
Function get_keyword ($ url, $ kw_start)
{
$ Start = stripos ($ url, $ kw_start );
$ Url = substr ($ url, $ start + strlen ($ kw_start ));
$ Start = stripos ($ url ,'&');
If ($ start> 0)
{
$ Start = stripos ($ url ,'&');
$ S_s_keyword = substr ($ url, 0, $ start );
}
Else
{
$ S_s_keyword = substr ($ url, 0 );
}
Return $ s_s_keyword;
}

$ Url = isset ($ _ SERVER ['http _ referer'])? $ _ SERVER ['http _ referer']: ''; // Get the inbound url.
$ Search_1 = "google.com"; // q = utf8
$ Search_2 = "baidu.com"; // wd = gbk
$ Search_3 = "yahoo.cn"; // q = utf8
$ Search_4 = "sogou.com"; // query = gbk
$ Search_5 = "soso.com"; // w = gbk
$ Search_6 = "bing.com"; // q = utf8
$ Search_7 = "youdao.com"; // q = utf8

$ Google = preg_match ("/\ B {$ search_1} \ B/", $ url); // record matching information for inbound judgment.
$ Baidu = preg_match ("/\ B {$ search_2} \ B/", $ url );
$ Yahoo = preg_match ("/\ B {$ search_3} \ B/", $ url );
$ Sogou = preg_match ("/\ B {$ search_4} \ B/", $ url );
$ Soso = preg_match ("/\ B {$ search_5} \ B/", $ url );
$ Bing = preg_match ("/\ B {$ search_6} \ B/", $ url );
$ Youdao = preg_match ("/\ B {$ search_7} \ B/", $ url );
$ S_s_keyword = "";
$ Bul = $ _ SERVER ['http _ referer'];
// Retrieve domain names without parameters
Preg_match ('@ ^ (? : Http ://)? ([^/] +) @ I ', $ bul, $ matches );
$ Burl = $ matches [1];
// Match Domain Name settings
$ Curl = "www.netxu.com ";
If ($ burl! = $ Curl ){
If ($ google)
{// From google
$ S_s_keyword = get_keyword ($ url, 'Q = '); // the character before the keyword is "q = ".
$ S_s_keyword = urldecode ($ s_s_keyword );
$ Urlname = "Google :";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
// $ S_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk
}
Else if ($ baidu)
{// From Baidu
$ S_s_keyword = get_keyword ($ url, 'wd = '); // the character before the keyword is "wd = ".
$ S_s_keyword = urldecode ($ s_s_keyword );
$ S_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk
$ Urlname = "Baidu :";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
}
Else if ($ yahoo)
{// From Yahoo
$ S_s_keyword = get_keyword ($ url, 'Q = '); // the character before the keyword is "q = ".
$ S_s_keyword = urldecode ($ s_s_keyword );
// $ S_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk
$ Urlname = "Yahoo :";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
}
Else if ($ sogou)
{// From Sogou
$ S_s_keyword = get_keyword ($ url, 'query = '); // the character before the keyword is "query = ".
$ S_s_keyword = urldecode ($ s_s_keyword );
$ S_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk
$ Urlname = "Sogou :";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
}
Else if ($ soso)
{// From sousearch
$ S_s_keyword = get_keyword ($ url, 'W = '); // the character before the keyword is "w = ".
$ S_s_keyword = urldecode ($ s_s_keyword );
$ S_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk
$ Urlname = "search :";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
}
Else if ($ bing)
{// From Bing
$ S_s_keyword = get_keyword ($ url, 'Q = '); // the character before the keyword is "q = ".
$ S_s_keyword = urldecode ($ s_s_keyword );
// $ S_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk
$ Urlname = "Bing :";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
}
Else if ($ youdao)
{// From Youdao
$ S_s_keyword = get_keyword ($ url, 'Q = '); // the character before the keyword is "q = ".
$ S_s_keyword = urldecode ($ s_s_keyword );
// $ S_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk
$ Urlname = "youdao :";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
}
Else {
$ Urlname = $ burl;
$ S_s_keyword = "";
$ _ SESSION ["urlname"] = $ urlname;
$ _ SESSION ["s_s_keyword"] = $ s_s_keyword;
}
$ S_urlname = $ urlname;
$ S_urlkey = $ s_s_keyword;
}
Else {
$ S_urlname = $ _ SESSION ["urlname"];
$ S_urlkey = $ _ SESSION ["s_s_keyword"];
}
?>

One thing to remember is that because the page encoding of different search engines is different, some are GBK, some are UTF8, so there will be different character encoding conversions during retrieval.
Reprinted please note: http://www.netxu.com/program/12.html

The following is another function. For more information, see
The code is as follows:
$ _ SERVER ['http _ referer'] = 'http: // www.baidu.com/s? Wd = http://www.bitsCN.com ';
Echo save_www_iiwnet_com_keyword ('http: // www.baidu.com/s? Wd = http://www.bitsCN.com ', 'http: // www.baidu.com/s? Wd = http://www.bitsCN.com ');
Function save_www_iiwnet_com_keyword ($ domain, $ path ){
If (strpos ($ domain, 'Google .com.tw ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'Google TAIWAN ';
$ Keywords = urldecode ($ regs [1]); // google taiwan
}
If (strpos ($ domain, 'google. cn ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'Google China ';
$ Keywords = urldecode ($ regs [1]); // google china
}
If (strpos ($ domain, 'google. com ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'Google ';
$ Keywords = urldecode ($ regs [1]); // google
} Elseif (strpos ($ domain, 'Baidu .')! = False & preg_match ('/wd = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'baidu ';
$ Keywords = urldecode ($ regs [1]); // baidu
} Elseif (strpos ($ domain, 'Baidu .')! = False & preg_match ('/word = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'baidu ';
$ Keywords = urldecode ($ regs [1]); // baidu
} Elseif (strpos ($ domain, '2017 .vnet.cn ')! = False & preg_match ('/kw = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'ct114 ';
$ Keywords = urldecode ($ regs [1]); // ct114
} Elseif (strpos ($ domain, 'iask. com ')! = False & preg_match ('/k = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'iask ';
$ Keywords = urldecode ($ regs [1]); // iask
} Elseif (strpos ($ domain, 'soso. com ')! = False & preg_match ('/w = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'soso ';
$ Keywords = urldecode ($ regs [1]); // soso
} Elseif (strpos ($ domain, 'sogou. com ')! = False & preg_match ('/query = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'sogou ';
$ Keywords = urldecode ($ regs [1]); // sogou
} Elseif (strpos ($ domain, 'So .163.com ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'deletease ';
$ Keywords = urldecode ($ regs [1]); // netease
} Elseif (strpos ($ domain, 'yodao. com ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'yodao ';
$ Keywords = urldecode ($ regs [1]); // yodao
} Elseif (strpos ($ domain, 'zhongsou. com ')! = False & preg_match ('/word = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'zhongsou ';
$ Keywords = urldecode ($ regs [1]); // zhongsou
} Elseif (strpos ($ domain, 'search .tom.com ')! = False & preg_match ('/w = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'Tom ';
$ Keywords = urldecode ($ regs [1]); // tom
} Elseif (strpos ($ domain, 'Live. com ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'mslive ';
$ Keywords = urldecode ($ regs [1]); // MSLIVE
} Elseif (strpos ($ domain, 'tw .search.yahoo.com ')! = False & preg_match ('/p = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'Yahoo TAIWAN ';
$ Keywords = urldecode ($ regs [1]); // yahoo taiwan
} Elseif (strpos ($ domain, 'CN. yahoo .')! = False & preg_match ('/p = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'Yahoo China ';
$ Keywords = urldecode ($ regs [1]); // yahoo china
} Elseif (strpos ($ domain, 'Yahoo .')! = False & preg_match ('/p = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'Yahoo ';
$ Keywords = urldecode ($ regs [1]); // yahoo
} Elseif (strpos ($ domain, 'MSN .com.tw ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'MSN TAIWAN ';
$ Keywords = urldecode ($ regs [1]); // msn taiwan
} Elseif (strpos ($ domain, 'MSN .com.cn ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'MSN China ';
$ Keywords = urldecode ($ regs [1]); // msn china
} Elseif (strpos ($ domain, 'MSN. com ')! = False & preg_match ('/q = ([^ &] *)/I', $ path, $ regs )){
$ Searchengine = 'MSN ';
$ Keywords = urldecode ($ regs [1]); // msn
}
Return $ keywords;
}

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.