Home Backend Development PHP Tutorial 使用php显示搜索引擎来的关键词_php实例

使用php显示搜索引擎来的关键词_php实例

May 17, 2016 am 08:49 AM
Key words search engine

以下是相关实现代码:

复制代码 代码如下:

/*
Plugin Name: display-search-keywords
Plugin URI: http://www.imyxiao.com/1531.html
Description: 当访客通过搜索引擎来到你的博客,这个插件可以显示访客搜索的关键词
Version: 1.0
Author:仰肖
*/
function unescape($str) {
 $ret = '';
 $len = strlen($str);
 for ($i = 0; $i   if ($str[$i] == '%' && $str[$i +1] == 'u') {
   $val = hexdec(substr($str, $i +2, 4));
   if ($val     $ret .= chr($val);
   else
    if ($val      $ret .= chr(0xc0 | ($val >> 6)) . chr(0x80 | ($val & 0x3f));
    else
     $ret .= chr(0xe0 | ($val >> 12)) . chr(0x80 | (($val >> 6) & 0x3f)) . chr(0x80 | ($val & 0x3f));

   $i += 5;
  } else
   if ($str[$i] == '%') {
    $ret .= urldecode(substr($str, $i, 3));
    $i += 2;
   } else
    $ret .= $str[$i];
 }
 return $ret;
}
function ls_get_delim($ref) {
 $search_engines = array (
  'google.com.hk' => 'q',
  'google.com.tw' => 'q',
  'go.google.com' => 'q',
  'google.com' => 'q',
  'blogsearch.google.com' => 'q',
  'cn.bing.com' => 'q',
  'one.cn.yahoo.com' => 'p',
  'baidu.com' => 'wd',
  'soso.com' => 'w',
  'youdao.com' => 'q',
  'sogou.com' => 'query'
 );
 $delim = false;
 // 判断
 if (isset ($search_engines[$ref])) {
  $delim = $search_engines[$ref];
 }
 return $delim;
}

function ls_get_refer() {
 // 判断前一页面的 URL 地址
 $queryString = $_GET['referer'];
 $queryString = unescape($queryString);
 if (!isset ($queryString) || ($queryString == ''))
  return false;
 $referer_info = parse_url($queryString);
 $referer = $referer_info['host'];
 //去除www.
 if (substr($referer, 0, 4) == 'www.')
  $referer = substr($referer, 4);
 return $referer;
}

function ls_getinfo($what) {
 $referer = ls_get_refer();
 if (!$referer)
  return false;
 $delimiter = ls_get_delim($referer);
 if ($delimiter) {
  $terms = ls_get_terms($delimiter);
  if ($what == 'isref' && $terms != '') {
   return true;
  }
  if ($what == 'terms') {
   echo $terms;
  }
 }
 return false;
}

function yxiao_seems_utf8($str) {
 $length = strlen($str);
 for ($i = 0; $i   $c = ord($str[$i]);
  if ($c    $n = 0; # 0bbbbbbb
  elseif (($c & 0xE0) == 0xC0) $n = 1; # 110bbbbb
  elseif (($c & 0xF0) == 0xE0) $n = 2; # 1110bbbb
  elseif (($c & 0xF8) == 0xF0) $n = 3; # 11110bbb
  elseif (($c & 0xFC) == 0xF8) $n = 4; # 111110bb
  elseif (($c & 0xFE) == 0xFC) $n = 5; # 1111110b
  else
   return false; # Does not match any model
  for ($j = 0; $j    if ((++ $i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
    return false;
  }
 }
 return true;
}

function ls_get_terms($d) {
 //取得查询值
 $queryString = $_GET['referer'];
 $queryString = unescape($queryString);
 $query_str = parse_url($queryString);
 parse_str($query_str[query], $query_str);
 $query = $query_str[$d];
 $query = urldecode($query);

 $query = str_replace("'", '', $query);
 $query = str_replace('"', '', $query);
 $query_array = preg_split('/[\s,\+\.]+/', $query);
 $query_terms = implode(' ', $query_array);
 $terms = htmlspecialchars($query_terms);
 //gbk->utf8
 if (!yxiao_seems_utf8($terms)) {
  $terms = iconv("GBK", "UTF-8//IGNORE", $terms);
 }
 return $terms;
}
if (ls_getinfo('isref')) {
?>
document.write('

')
document.write('更多搜索结果:document.write('title="查看的搜索结果" rel="nofollow">')
document.write('

');

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain what the explorer.exe process is Explain what the explorer.exe process is Feb 18, 2024 pm 12:11 PM

Explain what the explorer.exe process is

How to adjust aperture on Xiaomi Mi 14 Ultra? How to adjust aperture on Xiaomi Mi 14 Ultra? Mar 19, 2024 am 09:01 AM

How to adjust aperture on Xiaomi Mi 14 Ultra?

What is the highest graphics card that r5 5600x can drive? The latest performance of using 5600X with RX6800XT What is the highest graphics card that r5 5600x can drive? The latest performance of using 5600X with RX6800XT Feb 25, 2024 am 10:34 AM

What is the highest graphics card that r5 5600x can drive? The latest performance of using 5600X with RX6800XT

How to set Chinese in Cheat Engine? How to set Chinese in ce modifier How to set Chinese in Cheat Engine? How to set Chinese in ce modifier Mar 18, 2024 pm 01:20 PM

How to set Chinese in Cheat Engine? How to set Chinese in ce modifier

Which has a greater impact on performance, memory frequency or timing? Which has a greater impact on performance, memory frequency or timing? Feb 19, 2024 am 08:58 AM

Which has a greater impact on performance, memory frequency or timing?

What does the 0x0000004e error mean? What does the 0x0000004e error mean? Feb 18, 2024 pm 01:54 PM

What does the 0x0000004e error mean?

Baidu cloud disk search engine entrance Baidu cloud disk search engine entrance Feb 27, 2024 pm 01:00 PM

Baidu cloud disk search engine entrance

How to update Honor MagicOS 8.0 on Honor 90 GT? How to update Honor MagicOS 8.0 on Honor 90 GT? Mar 18, 2024 pm 06:46 PM

How to update Honor MagicOS 8.0 on Honor 90 GT?

See all articles