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

Source: Internet
Author: User

Recently, in a project, the customer needs a function. The customer needs to know the way in which the user places the order for the sale of a product. The customer service software (53 Customer Service) and webmaster statistics can only meet the working hours, and take off work in the evening and take a rest on Saturday. The customer service is not online and the user does not know the specific source of the Order when placing an order. Therefore, you can only add a field through the website function to obtain the source keyword, how to obtain the source keyword, the code is published below, which contains the retrieval methods of several major search engines (Baidu, Google, Yahoo, sogou, search, Bing, and youdao), which are indicated in the code, hope to help you.

The Code is as follows:Copy codeThe Code is as follows: <? Php
// 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
Copy codeThe Code is as follows: <? Php
$ _ SERVER ['HTTP _ referer'] = 'HTTP: // www.baidu.com/s? Wd = http://www.jb51.net ';
Echo save_www_iiwnet_com_keyword ('HTTP: // www.baidu.com/s? Wd = http://www.jb51.net ', 'HTTP: // www.baidu.com/s? Wd = http://www.jb51.net ');
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.