获取搜索引擎来源关键词php示例
<?php //获取关键词 function GetKwd($rid,$query) { $wd = GetTT($rid); parse_str($query, $out); if(count($wd)>0) { while (list($key, $val) = each($out)) { if(in_array($key,$wd)) { $keyword=decodekeyword($val); } } } return $keyword; } //设置不同来源的搜索变量名 function GetTT($TID) { $arr=array( "1"=>array("wd","word"),//百度搜索时候的关键字变量名 "2"=>array("q") //谷歌搜索时候的关键字变量名 ); return $arr[$TID]; } //编码转换 function decodekeyword($kval) { $charset=mb_detect_encoding($kval,array("ASCII",'UTF-8',"GB2312","GBK",'BIG5')); if($charset!='UTF-8') { return iconv($charset,'UTF-8',$kval); } else { return $kval; } } $log_arr['reurl'] = urlencode($_GET['referrer']);//来源url $tmp_reurl = parse_url(urldecode($log_arr['reurl'])); $kwd = ''; if(stristr($tmp_reurl['host'],'baidu') == true || stristr($tmp_reurl['host'],'BAIDU') == true){ $log_arr['lyid'] = 1; $kwd = GetKwd(1,$tmp_reurl['query']); }elseif (stristr($tmp_reurl['host'],'google') == true || stristr($tmp_reurl['host'],'GOOGLE') == true){ $log_arr['lyid'] = 2; $kwd = GetKwd(2,$tmp_reurl['query']); } $log_arr['kwd'] = $kwd;