php控制访问人数的方法

<pre>
//添加访问人数
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();
}
</pre>

网站入口写
<pre>
addfangwen();
</pre>
还有js初始化的时候要长连接 swoole

swoole
<pre>
$server->on('close', function ($ser, $fd) {

$redis = \RedisClient::getInstance();
$redis->decr('gheliceshi1_fangwen');
if ($redis->get('gheliceshi1_fangwen') <= 0) {
$redis->set('gheliceshi1_fangwen', 0);
}

});
</pre>
关闭浏览器自然就是断开连接了 用下面方法减少人数

<pre>

jianshaofangwen();
</pre>

这样就可以使用贷款了 不怕不够用了

05-18 10:19