Home  >  Article  >  Backend Development  >  PHP+Memcache implements statistics on the total number of wordpress visits (non-plugin)_PHP tutorial

PHP+Memcache implements statistics on the total number of wordpress visits (non-plugin)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:34713browse

I wrote a blog post before, using PHP and Memcache to implement a website. Check it out at the following link: http://www.jb51.net/article/51825.htm
Today I will use this function in wordpress and save the number of visits to the database.

MySQL statement

First, add the default data of the number of visits in the parameter table

// 获取所有浏览次数
function get_all_visit_number()
{
 $mc = new Memcache ();
 
 // 使用wordpress自带wpdb类
 global $wpdb;
 
 // 参数表
 $table = "wp_options";
 
 // 连接memcache
 $mc->connect ( "127.0.0.1", 11211 );
 
 // 获取浏览次数
 $visit_number = $mc->get ( 'visit_number' );
 
 // Memcache 中是否存有访问次数
 if (!$visit_number) {

 // 不存在时,查询数据库 
 $querystr = "SELECT `option_value` FROM " .$table. " WHERE `option_name`='visit_number'";
 $results = $wpdb->get_results($querystr);
 
 // 把数据库中存储的值赋予memcache变量
 $visit_number = intval($results[0]->option_value);
 }
 
 // 设置浏览次数
 $mc->set ( 'visit_number', ++$visit_number);
 
 // 获取浏览次数
 $visit_number = $mc->get ( 'visit_number' );
 

 // 每达100次访问量,更新到数据库
 if ($visit_number % 100 == 0) {

 // 使用wordpress自带wpdb类
 $data_array = array(
  'option_value' => $visit_number
 );
 
 $where_clause = array(
  'option_name' => 'visit_number'
 );
 
 $wpdb->update($table,$data_array,$where_clause);
 }
 
 // 关闭memcache连接
 $mc->close ();
 
 return $visit_number;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824647.htmlTechArticleI have written a blog post before, using PHP and Memcache to implement a website. Check it out at the following link: http://www. jb51.net/article/51825.htm Today I will use this function in wordpress and realize the visit...
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