在php中,使用$_SERVER["HTTP_REFERER"]就能获得访客的HTTP Referer。里面记录了来源的url地址,各个搜索引擎都有自己的url规则,比如说谷歌的搜索关键词是跟在q=的后面,百度的则是wd=或 word=。需要注意的是一些中文搜索引擎他使用的可能是gb2312或gbk编码,和wordpress的utf-8的不一样,获取关键词时就需要进行 转码。

下表总结了常见的搜索引擎的url规则和字符编码:

搜索引擎

关键词变量名

编码

google

q

由url中的ie字段指定

baidu

wd或word

gb2312或ie字段

yahoo

p或keyword

ei字段指定

sogou

query

gb2312

bing

q

utf-8

youdao

q或keyword

ue字段指定

soso

w


gb2312



$refer = $_SERVER["HTTP_REFERER"]; $refer_string = parse_url($refer, PHP_URL_QUERY); parse_str($refer_string, $vars); if (strpos($refer, "google")) {     $search_terms = $vars['q'];     $bIsUTF8 = ($vars["ie"] == "GB") ? false : true;  } else if(strpos($refer, "baidu")) {     $search_terms = $vars['wd']?$vars['wd']:$vars['word'];     $bIsUTF8 = (strtolower($vars["ie"]) == "utf-8") ? true : false;   } else if(strpos($refer, "yahoo")) {     $search_terms = $vars['p']?$vars['p']:$vars['keyword'];     $bIsUTF8 = (strtolower($vars["ei"]) == "utf-8") ? true : false; } else if(strpos($refer, "sogou")) {     $search_terms = $vars["query"];     $bIsUTF8 = false; } else if(strpos($refer, "bing")) {     $search_terms = $vars['q'];     $bIsUTF8 = true;   } else if(strpos($refer, "youdao")) {     $search_terms = $vars['q']?$vars['q']:$vars['keyword'];     $bIsUTF8 = (strtolower($vars["ue"]) == "utf8") ? true : false; } else if(strpos($refer, "soso")) {     $search_terms = $vars["w"];     $bIsUTF8 = false; }   if(!$bIsUTF8) $search_terms =iconv("gb2312","utf-8",$search_terms);