<?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