0
我正在使用PHP做一個簡單的遊戲。PHP如何保存值餅乾/會話
我需要存儲某些東西,比如遊戲正在進行什麼以及總共等等,並保存此數據並稍後訪問(在另一頁上或當我回到此頁時)
我嘗試過使用會話,但這並不適合我。
我可以使用cookie來做到這一點嗎?
這是我第一次使用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>
這個問題有點不同。我在問我該怎麼做。 – js091514
我想可能會議不是要走的路。 – js091514