Home > Backend Development > PHP Tutorial > PHP implementation code for recording search engine origins and keywords

PHP implementation code for recording search engine origins and keywords

WBOY
Release: 2016-07-25 08:57:31
Original
1006 people have browsed it
This article introduces a piece of code implemented in PHP that can record the source and keywords of search engines. Friends in need can refer to it.

Code:

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

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

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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template