<?php /** * 分析搜索引擎到来的关键字 * * * @author zhangjun * @charset utf-8 */ class searchTerms { public static $banUrl = array('qq.com','localhost'); //不解析的referer 有利于加快程序处理速度 public static $parseHost = array('baidu','google','360','soso'); //能解析的referer /** * 主方法 * @return boolean || array */ public static function keyword(){ if(!isset($_SERVER['HTTP_REFERER'])) return false; $referer = trim($_SERVER['HTTP_REFERER']); $refererArr = parse_url($referer); //判断refer是否来至不需要分析的地址。 if(self::inBanUrl($refererArr['host'])) return false; $hasParseFun = false; foreach(self::$parseHost as $host) { if(strpos ($refererArr['host'],$host) !== false) { $hasParseFun = true; break; } } if(!$hasParseFun) return false; $queryVars = array(); parse_str($refererArr['query'], $queryVars); //调用每个搜索引擎的单独处理方法 $method = 'parse'.ucfirst($host); return self::$method($queryVars); } /* 已取消使用正则的方式。 public static function buldPattern($rule) { $host = str_replace('.', '.', $rule['host']); $patterns = array(); foreach ($rule['key'] as $key) { $patterns[] = $host . '.+?'.$key.'='; } return "/(?:" . implode('|', $patterns).')([^&]*)/'; }*/ public static function inBanUrl($referer) { foreach(self::$banUrl as $url) { if(strpos($url, $referer) !== false) return true; } return false; } public static function parseBaidu($params) { $searchTerms = ''; if(isset($params['kw'])) { $searchTerms = $params['kw']; } else if(isset ($params['wd'])) { $searchTerms = $params['wd']; } else if(isset ($params['word'])) { $searchTerms = $params['word']; } return isset($params['ie']) && (strtolower($params['ie']) == 'utf-8') ? $searchTerms : iconv('gbk', 'utf-8', $searchTerms); } public static function parseGoogle($params) { $searchTerms = ''; if(isset($params['q'])) { $searchTerms = $params['q']; } return isset($params['ie']) && ($params['ie'] == "GB") ? iconv('gbk', 'utf-8', $searchTerms) : $params['q']; } public static function parse360($params) { $searchTerms = ''; if(isset($params['q'])) { $searchTerms = $params['q']; } return $searchTerms; } public static function parseSoso($params) { $searchTerms = ''; if(isset($params['w'])) { $searchTerms = $params['w']; } return iconv('gbk', 'utf-8', $searchTerms); } }