PHP access to search engine keywords from the source of functions (support Baidu and Google and other search engines) _php Tutorial

Source: Internet
Author: User
Recently encountered a project, the customer needs a function, a product sales order need to know the user from which channel, think customer service software (53 customer service) and webmaster statistics can only meet the working time, and night work and week 6th rest, customer service is not the line, the user will not know the specific source of the order, so, Also can only through the site function to add a field, get the source keyword, how to get the source keyword, the code is sent below, which contains (Baidu, Google, Yahoo, Sogou, search, Bing, Youdao) several major search engine access method, the code is noted, I hope you have help, share

The code is as follows:
Copy CodeThe code is as follows:
Get keywords from search engine inbound
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 match 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 '];
Get the no parameter domain name
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);//engine for 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);//engine for 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);//engine for 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);//engine for GBK
$urlname = "Sogou:";
$_session["urlname"]= $urlname;
$_session["S_s_keyword"]= $s _s_keyword;
}
else if ($soso)
{//From Search
$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);//engine for 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);//engine for 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);//engine for 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, because each search engine page encoding is not the same, there are some gbk, there are some UTF8, so in the acquisition will have different character encoding conversion
Reprint Please specify: http://www.netxu.com/program/12.html

here is another function, you can also refer to the following
Copy CodeThe code is as follows:
$_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, ' 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, ' 114.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 = ' NetEase ';
$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;
}

http://www.bkjia.com/PHPjc/326106.html www.bkjia.com true http://www.bkjia.com/PHPjc/326106.html techarticle recently encountered a project, the customer needs a function, a product sales order needs to know the user is from which channel, think customer service software (53 customer service) and webmaster statistics can only full ...

  • Related Article

    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.