php用session实现多并发抢购不多卖
程序员文章站
2022-03-11 08:09:09
...
php代码:
<?php session_id(md5($id));//用id做为sessionid session_start(); if(!isset($_SESSION['i'])){ $_SESSION['i'] = i();//获取库存 } if($_SESSION['i'] > 0){ $buy = true; $_SESSION['i']--; }else{ $buy = false; } session_write_close(); if(isset($_COOKIE['PHPSESSID'])){//还原原来的session session_id($_COOKIE['PHPSESSID']); session_start(); }