Home >php教程 >PHP源码 >PHP记录搜索引擎来路关键字

PHP记录搜索引擎来路关键字

PHP中文网
PHP中文网Original
2016-05-25 17:04:111063browse


<?php
/**
* 记录搜索引擎来路及关键词
* edit by www.jbxue.com
*/
//搜索引擎来路
function keywords($url){
$spier=array(&#39;baidu.&#39;=>&#39;百度&#39;,&#39;google.&#39;=>&#39;谷歌&#39;,&#39;soso.&#39;=>&#39;搜搜&#39;,&#39;sogou.&#39;=>&#39;搜狗&#39;,&#39;www.so.com&#39;=>&#39;360&#39;);
$q=array(&#39;百度&#39;=>&#39;/wd=([^&]*)/i&#39;,&#39;谷歌&#39;=>&#39;/q=([^&]*)/i&#39;,&#39;360&#39;=>&#39;/q=(.*)/i&#39;,&#39;搜狗&#39;=>&#39;/query=([^&]*)/i&#39;,&#39;搜搜&#39;=>&#39;/w=([^&]*)/i&#39;);
foreach($spier as $k=>$v){
   if(strpos($url,$k)){
      preg_match("{$q[$v]}",$url,$b);
      if($v==&#39;搜搜&#39;||$v==&#39;搜狗&#39;){
 $keywords=iconv(&#39;GBK&#39;,&#39;UTF-8&#39;,urldecode($b[1]));
      }else{
 $keywords=urldecode($b[1]);
}

echo "来自{$v}的关键字:".$keywords;
 }
} 
}
$url=$_SERVER[&#39;HTTP_REFERER&#39;]; //这个放到网站的头部来获取来访的地址的
      
keywords($url);

//记录用户搜索引擎关键词代码
//记录用户通过常见的搜索引擎搜索关键词
$rfr = $_SERVER[&#39;HTTP_REFERER&#39;];
//if(!$rfr) $rfr=&#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;];
if($rfr)
{
 $p=parse_url($rfr);
 parse_str($p[&#39;query&#39;],$pa);
 $p[&#39;host&#39;]=strtolower($p[&#39;host&#39;]);
 $arr_sd_key=array(
 &#39;baidu.com&#39;=>&#39;word&#39;,
 &#39;google.com&#39;=>&#39;q&#39;,
 &#39;sina.com.cn&#39;=>&#39;word&#39;,
 &#39;sohu.com&#39;=>&#39;word&#39;,
 &#39;msn.com&#39;=>&#39;q&#39;,
 &#39;bing.com&#39;=>&#39;q&#39;,
 &#39;163.com&#39;=>&#39;q&#39;,
 &#39;yahoo.com&#39;=>&#39;p&#39;
 );
 $keyword=&#39;&#39;;
 $sengine=$p[&#39;host&#39;];
 foreach($arr_sd_key as $se=>$kwd)
 {
if(strpos($p[&#39;host&#39;],$se)!==false)
{
 $keyword=$pa[$kwd];
 $sengine=$se;
 break;
}
 }
 $sql="insert into visit_log(domain,key_word,ct)";
}
?>

                   

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