PHP自定义函数获取搜索引擎来源关键字的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP自定义函数获取搜索引擎来源关键字的方法。分享给大家供大家参考,具体如下:

获取搜索引擎来源关键字的函数:

function getKeywords() {
  // 搜索引擎关键字映射
  static $host_keyword_map = array(
      'www.baidu.com' => 'wd',
      'v.baidu.com' => 'word',
      'image.baidu.com' => 'word',
      'news.baidu.com' => 'word',
      'www.so.com' => 'q',
      'video.so.com' => 'q',
      'image.so.com' => 'q',
      'news.so.com' => 'q',
      'www.sogou.com' => 'query',
      'pic.sogou.com' => 'query',
      'v.sogou.com' => 'query',
  );
  // 检查来源是否搜索引擎
  if (!isset($_SERVER['HTTP_REFERER'])) {
    return '';
  }
  $urls = parse_url($_SERVER['HTTP_REFERER']);
  if (!array_key_exists($urls['host'], $host_keyword_map)) {
    return '';
  }
  $key = $host_keyword_map[$urls['host']];
  // 检查关键字参数是否存在
  if (!isset($urls['query'])) {
    return '';
  }
  $params = array();
  parse_str($urls['query'], $params);
  if (!isset($params[$key])) {
    return '';
  }
  $keywords = $params[$key];
  // 检查编码
  $encoding = mb_detect_encoding($keywords, 'utf-8,gbk');
  if ($encoding != 'utf-8') {
    $keywords = iconv($encoding, 'utf-8', $keywords);
  }
  return $keywords;
}

函数测试:

<?php
header("Content-Type: text/html; charset=utf-8");
$referers = array(
    'http://www.baidu.com/s?cl=3&wd=%B9%E9%C0%B4&fr=vid1000',
    'http://www.baidu.com/s?tn=92506501_hao_pg&rtt=1&bsst=1&wd=%B9%E9%C0%B4',
    'http://www.baidu.com/link?url=ctBhF7AAau6LwE61pJOEH-ZhgUM7D3YHYMrm6xIXJlDQtMXCiea7gg49s90Q-Qh8wHD8Ano-dPNhUawBBNEEwEbtu8toMF5k1V7Xy850EtlpZyMcS0e_y-SCJp86iM6e&wd=%E5%BD%92%E6%9D%A5&tn=baidu&ie=utf-8&inputT=2980',
    'http://www.baidu.com/link?url=TIn9NR6fwiy6IwwkCcVF8HhHoxVUpHQsyj1YdlQPy2roXKTnSQS_3UxwvyjZ2JPkpxF8-diSoRCSpODUM_jq2K&wd=%E5%BD%92%E6%9D%A5&tn=baidu&ie=utf-8&input', 
    'http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=%E5%BD%92%E6%9D%A5&ie=utf-8',
    '/zb_users/upload/202003/afdzubx3uwh.html',
    'http://v.baidu.com/v?ct=301989888&s=25&ie=utf-8&word=%E5%BD%92%E6%9D%A5',
    'http://www.so.com/s?ie=utf-8&shb=1&src=360sou_newhome&q=%E5%BD%92%E6%9D%A5',
    'http://video.so.com/v?q=%E5%BD%92%E6%9D%A5&src=tab_www',
    'http://image.so.com/v?q=%E5%BD%92%E6%9D%A5&src=tab_video&fromurl=http%3A%2F%2Fndent.oeeee.com%2Fhtml%2F201309%2F16%2F258899.html',
    'http://news.so.com/ns?q=%E5%BD%92%E6%9D%A5&src=tab_video',
    'http://www.sogou.com/web?query=%E5%BD%92%E6%9D%A5&_asf=www.sogou.com&_ast=1402284372&w=01019900&p=40040100&ie=utf8&sut=6558&sst0=1402284372272&lkt=0%2C0%2C0',
    'http://www.sogou.com/web?query=%E5%BD%92%E6%9D%A5&_asf=www.sogou.com&_ast=1402284372&w=01019900&p=40040100&ie=utf8&sut=6558&sst0=1402284372272&lkt=0%2C0%2C0',
    'http://pic.sogou.com/d?query=%B9%E9%C0%B4&mood=0&picformat=0&mode=1&di=0&w=03021800&dr=1&did=1',
    'http://v.sogou.com/v?query=%B9%E9%C0%B4&p=&w=',
    'http://www.baidu.com/s?aaa=bbb',
    'http://www.baidu.com/',
    '//www.jb51.net/',
);
foreach ($referers as $r) {
  $_SERVER['HTTP_REFERER'] = $r;
  echo getKeywords(), "\n";
}

搜索引擎占有比率:

http://engine.data.cnzz.com/

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP错误与异常处理方法总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP中读取照片exif信息的方法

PHP中读取照片exif信息的方法

先来了解什么是图片的Exif信息 Exif是一种图象文件格式,它的数据存储与JPEG格式是完全相同的。实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈、快门...

php源代码安装常见错误与解决办法分享

错误:configure: error: libevent >= 1.4.11 could not be found 解决:yum -y install libevent libe...

PHP文件缓存内容保存格式实例分析

本文实例讲述了PHP文件缓存内容保存格式,对于进行PHP项目开发非常具有实用价值。分享给大家供大家参考借鉴。具体分析如下: 1、PHP文件缓存内容保存格式 PHP文件缓存内容保存格式主要...

关于php连接mssql:pdo odbc sql server

只有一个php_pdo_odbc.dll。 so~最新最好的php连接mssql方法应该是这样: 复制代码 代码如下: <?php $cnx = new PDO("odbc:Dri...

兼容性比较好的PHP生成缩略图的代码

复制代码 代码如下: function ImageResize($srcFile,$toW,$toH,$toFile="") { if($toFile==""){ $toFile = $...