我正在使用PHP制作一个简单的游戏。
我需要存储某些内容,例如游戏的轮次以及总数等等,并保存这些数据并稍后访问它(在另一页或我回到此页面时)
我尝试过使用会话,但这对我没用。
我可以使用cookies吗?
这是我第一次使用PHP。
这是我的代码,如果它有助于理解:
<form method="get" action= "skunk.php">
<h1>PLAY SKUNK</h1>
ROLL AGAIN? <br>
yes<input type="radio" name="role2" value="yes"/>
no<input type="radio" name="role2" value="no"/>
<br>
<input type="submit"/>
</form>
<?php
session_start();
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
$_SESSION["n"] = array(0);
$_SESSION["k2"] = array(0);
$rand = 0;
$rand2 = 0;
$_SESSION["round"] = 1;
if($_REQUEST["role2"] == "yes"){
$rand = rand (1, 6);
$rand2 = rand(1, 6);
if($rand == 1 and $rand ==1){
switch ($_SESSION["round"]) {
case 1:
$_SESSION["s"] = array(0);
break;
case 2:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
break;
case 3:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
break;
case 4:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
$_SESSION["n"] = array(0);
break;
case 5:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
$_SESSION["n"] = array(0);
$_SESSION["k2"] = array(0);
break;
}
}
if($rand > 1 and $rand2 > 1) {
switch ($_SESSION["round"]) {
case 1:
array_push($_SESSION["s"], $rand, $rand2);
break;
case 2:
array_push($_SESSION["k1"], $rand, $rand2);
break;
case 3:
array_push($_SESSION["u"], $rand, $rand2);
break;
case 4:
array_push($_SESSION["n"], $rand, $rand2);
break;
case 5:
array_push($_SESSION["k2"], $rand, $rand2);
break;
}
$_SESSION["round"]++;
}
if($_SESSION["round"] > 5){
session_destroy();
}
echo "you rolled: " . $rand . "\n";
echo "you rolled: " . $rand2;
}
?>
<table style="width:100%" border="1" >
<tr>
<td>S</td>
<td>K</td>
<td>U</td>
<td>N</td>
<td>K</td>
</tr>
</table>
<table style="width:100%" border="1" >
<tr>
<td><?php echo array_sum($_SESSION["s"]); ?></td>
<td><?php echo array_sum($_SESSION["k1"]); ?></td>
<td><?php echo array_sum($_SESSION["u"]); ?></td>
<td><?php echo array_sum($_SESSION["n"]); ?></td>
<td><?php echo array_sum($_SESSION["k2"]); ?></td>
</tr>
</table>
答案 0 :(得分:0)
我会将会话仅用于用户日志状态检查,并将所有其他数据存储到数据库中,这样可以更容易管理并更加一致。