php控制访问人数的方法
//添加访问人数
public function addfangwen()
{
header("Content-type:text/html;charset=utf-8");
$redis = /RedisClient::getInstance();
$moban_fangwen = $redis->get('gheliceshi1_fangwen');
if ($moban_fangwen >= 10) {
echo '<script>alert("访问人数过多,请稍后访问")</script>';
exit();
}else{
$redis->incr('gheliceshi1_fangwen');
}
}
//减少访问人数
public function jianshaofangwen()
{
$redis = /RedisClient::getInstance();
$redis->decr('gheliceshi1_fangwen');
if ($redis->get('gheliceshi1_fangwen') <= 0) {
$redis->set('gheliceshi1_fangwen', 0);
}
print_r($redis->get('gheliceshi1_fangwen'));
exit();
}
网站入口写
addfangwen();
还有js初始化的时候要长连接 swoole
swoole
$server->on('close', function ($ser, $fd) {$redis = /RedisClient::getInstance();
$redis->decr('gheliceshi1_fangwen');
if ($redis->get('gheliceshi1_fangwen') <= 0) {
$redis->set('gheliceshi1_fangwen', 0);
}});
关闭浏览器自然就是断开连接了 用下面方法减少人数
jianshaofangwen();
这样就可以使用贷款了 不怕不够用了
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/19099.html