这是我正在尝试做的事情:
function increment_users($id){
$rooms = $_SESSION['rooms_array'];
//var_dump($rooms);
foreach ($rooms as $room) {
if($room['id'] == $id){$room['users']=$room['users']+1;}
}
session_write_close();
}
房间的用户没有增加。我检查了服务器的会话设置。?已经谢谢了。
这是整个代码,请帮助:
ini_set(' session.save_path','C:/session');
session_start();
$requested_room_id = $_REQUEST['new_room_id'];
//echo $requested_room_id;
function create_new_room($id, $current_users) {
$room = array();
$room ['id'] = $id;
$room ['users'] = $current_users;
$_SESSION['rooms_array'][]=$room;
}
function increment_users($id){
$rooms = $_SESSION['rooms_array'];
//var_dump($rooms);
foreach ($rooms as $room) {
if($room['id'] == $id){$room['users']=$room['users']+1;}
}
$_SESSION['rooms_array'] = $rooms;
}
$rooms = $_SESSION['rooms_array'];
$room_available = false;
$room_available_ids = array();
var_dump($rooms);
if ($rooms) { //check if there is a room with space
foreach ( $rooms as $room =>$val ) {
//echo $val['users'];
if ($val ['users'] < 4) {
$room_available = true;
$room_available_ids [] = $val ['id'];
}
}
}
if ($room_available) { //if there is a room available, add requesting client to the room
$room_available_id = array_pop ( $room_available_ids );
increment_users($room_available_id);
echo $room_available_id;
}
else { //create a new room otherwise
$rand11=rand(1000000, 99999999);
if(!$room_available) {$new_room = create_new_room($rand11, 1);}
echo $rand11;
}
代码很好。对我来说,我只是找不到问题所在。这是代码中 var_dump 转储的数组:
array(1) {
[0]=>
array(2) {
["id"]=>
int(63594055)
["users"]=>
int(1)
}
}