PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)
程序员文章站
2022-07-07 08:37:58
最近遇到一个项目,客户需要一个功能,一个商品销售下单需要知道用户下单是从哪个途径来的,认为客服软件(53客服)和站长统计只能满足工作时间,及晚上下班和周六日休息,客服不在线...
最近遇到一个项目,客户需要一个功能,一个商品销售下单需要知道用户下单是从哪个途径来的,认为客服软件(53客服)和站长统计只能满足工作时间,及晚上下班和周六日休息,客服不在线,用户下单会不知道该订单的具体来源,所以嘛,又只能通过网站功能来增加一字段,获取来源关键字,如何获取来源关键字,代码发在下面,里面包含(百度、谷歌、雅虎、搜狗、搜搜、必应、有道)几大搜索引擎的获取方法,代码中均注明,希望对你有帮助,分享一下
代码如下:
<?php
//获取来自搜索引擎入站时的关键词
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']:'';//获取入站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);//记录匹配情况,用于入站判断。
$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'];
//获取没参数域名
preg_match('@^(?:http://)?([^/]+)@i',$bul,$matches);
$burl=$matches[1];
//匹配域名设置
$curl="www.netxu.com";
if($burl!=$curl){
if ($google)
{//来自google
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
$urlname="谷歌:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
}
else if($baidu)
{//来自百度
$s_s_keyword=get_keyword($url,'wd=');//关键词前的字符为"wd="。
$s_s_keyword=urldecode($s_s_keyword);
$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="百度:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($yahoo)
{//来自雅虎
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="雅虎:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($sogou)
{//来自搜狗
$s_s_keyword=get_keyword($url,'query=');//关键词前的字符为"query="。
$s_s_keyword=urldecode($s_s_keyword);
$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="搜狗:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($soso)
{//来自搜搜
$s_s_keyword=get_keyword($url,'w=');//关键词前的字符为"w="。
$s_s_keyword=urldecode($s_s_keyword);
$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="搜搜:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($bing)
{//来自必应
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="必应:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($youdao)
{//来自有道
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="有道:";
$_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"];
}
?>
有一点要记住,由于各搜索引擎的页面编码不一样,有的是gbk,有的是utf8,所以在获取时会有不同对字符编码的转换
转载请注明:http://www.netxu.com/program/12.html
下面是另外一个函数,也可以参考下
<?php
$_server['http_referer']='http://www.baidu.com/s?wd=//www.jb51.net';
echo save_www_iiwnet_com_keyword('http://www.baidu.com/s?wd=//www.jb51.net','http://www.baidu.com/s?wd=//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 *';
$keywords = urldecode($regs[1]); // google *
}
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 *';
$keywords = urldecode($regs[1]); // yahoo *
}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 *';
$keywords = urldecode($regs[1]); // msn *
}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;
}
代码如下:
复制代码 代码如下:
<?php
//获取来自搜索引擎入站时的关键词
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']:'';//获取入站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);//记录匹配情况,用于入站判断。
$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'];
//获取没参数域名
preg_match('@^(?:http://)?([^/]+)@i',$bul,$matches);
$burl=$matches[1];
//匹配域名设置
$curl="www.netxu.com";
if($burl!=$curl){
if ($google)
{//来自google
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
$urlname="谷歌:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
}
else if($baidu)
{//来自百度
$s_s_keyword=get_keyword($url,'wd=');//关键词前的字符为"wd="。
$s_s_keyword=urldecode($s_s_keyword);
$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="百度:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($yahoo)
{//来自雅虎
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="雅虎:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($sogou)
{//来自搜狗
$s_s_keyword=get_keyword($url,'query=');//关键词前的字符为"query="。
$s_s_keyword=urldecode($s_s_keyword);
$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="搜狗:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($soso)
{//来自搜搜
$s_s_keyword=get_keyword($url,'w=');//关键词前的字符为"w="。
$s_s_keyword=urldecode($s_s_keyword);
$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="搜搜:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($bing)
{//来自必应
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="必应:";
$_session["urlname"]=$urlname;
$_session["s_s_keyword"]=$s_s_keyword;
}
else if($youdao)
{//来自有道
$s_s_keyword=get_keyword($url,'q=');//关键词前的字符为"q="。
$s_s_keyword=urldecode($s_s_keyword);
//$s_s_keyword=iconv("gbk","utf-8",$s_s_keyword);//引擎为gbk
$urlname="有道:";
$_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"];
}
?>
有一点要记住,由于各搜索引擎的页面编码不一样,有的是gbk,有的是utf8,所以在获取时会有不同对字符编码的转换
转载请注明:http://www.netxu.com/program/12.html
下面是另外一个函数,也可以参考下
复制代码 代码如下:
<?php
$_server['http_referer']='http://www.baidu.com/s?wd=//www.jb51.net';
echo save_www_iiwnet_com_keyword('http://www.baidu.com/s?wd=//www.jb51.net','http://www.baidu.com/s?wd=//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 *';
$keywords = urldecode($regs[1]); // google *
}
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 *';
$keywords = urldecode($regs[1]); // yahoo *
}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 *';
$keywords = urldecode($regs[1]); // msn *
}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;
}
推荐阅读
-
php获取从百度、谷歌等搜索引擎进入网站关键词的方法
-
PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)
-
php获取从百度、谷歌等搜索引擎进入网站关键词的方法
-
php获取从百度、谷歌等搜索引擎进入网站关键词的方法,谷歌关键词
-
php获取从百度、谷歌等搜索引擎进入网站关键词的方法_PHP
-
PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)_PHP
-
php获取从百度、谷歌等搜索引擎进入网站关键词的方法_php技巧
-
php获取搜索引擎关键字来源(支持百度、谷歌等搜索引擎)的函数
-
php获取搜索引擎关键字来源(支持百度、谷歌等搜索引擎)的函数
-
PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)_PHP