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;
}
$ 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 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.