Php+memcache implementation of the website online number of people statistics

Source: Internet
Author: User

<?PHP$MC=NewMemcache ();//Connection Memcache$MC->connect ("127.0.0.1",11211 ); //Get online user IP and online time data$online _members = $MCGet('online_members' ); //if NULL, initialize the dataif(!$online _members) {$online _members=array ();} //Get User IP$ip = $_server ["REMOTE_ADDR"]; //Reset online time for access users$online _members [$ip] =Time ();foreach($online _members as$k =$v) { //If the page is not visited after three minutes, it is just considered expired if(Time ()-$v > the) {unset ($online _members[$k]);}} //Reset Online User Data$mcSet('online_members', $online _members); //regain access to online user data$online _members = $MCGet('online_members' ); //Enter statistics online numberecho Count ($online _members);?>

  // 获取所有浏览次数 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 ; }

Web site online statistics implemented by Php+memcache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.