Home  >  Article  >  Backend Development  >  PHP function to obtain the source of search engine keywords (supports search engines such as Baidu and Google)_PHP tutorial

PHP function to obtain the source of search engine keywords (supports search engines such as Baidu and Google)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:15:21762browse

I recently encountered a project where the customer needed a function. To place an order for a product sales, you need to know which channel the user placed the order from. I think that the customer service software (53 customer service) and webmaster statistics can only meet the needs of working hours, evening get off work and weekdays. We are closed for six days and the customer service is not online. Users placing orders will not know the specific source of the order. Therefore, we can only add a field to obtain the source keyword through the website function. How to obtain the source keyword, the code is posted below. It contains the acquisition methods of several major search engines (Baidu, Google, Yahoo, Sogou, Soso, Bing, Youdao). They are all marked in the code. I hope it will be helpful to you. Share

The code is as follows :

Copy code The code is as follows:

//Get the inbound data from search engines Keywords
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 situation , used 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 domain name without parameters
preg_match('@^(?:http://)?([^/]+)@i',$bul ,$matches);
$burl=$matches[1];
//Matching 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 Soso
$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="Soso:" ;
$_SESSION["urlname"]=$urlname;
$_SESSION["s_s_keyword"]=$s_s_keyword;
}
else if($bing)
{//from bi Should
$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 since the page encoding of each search engine is different, some are GBK and some are UTF8, so there will be different character encoding conversions when retrieving
Please indicate: http://www .netxu.com/program/12.html

The following is another function, you can also refer to
Copy code The 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,'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,'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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326106.htmlTechArticleRecently encountered a project, the customer needs a function, a product sales order needs to know where the user placed the order from I came from this way, thinking that customer service software (53 customer service) and webmaster statistics can only satisfy...
Statement:
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