PHP 会话不适用于游戏 [英] PHP sessions not working for game
本文介绍了PHP 会话不适用于游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试模仿一个名为 SKUNK(你用骰子玩)的游戏来完成任务.
I am trying to imitate a game called SKUNK (you play it with dice) for an assignment.
我无法让会话工作.这是我第一次使用 PHP.我还被告知无需会话即可完成.
I can't get the sessions to work. This is my first time using PHP. Also I was told it can be done without sessions.
这是我的代码:
<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>
推荐答案
如果你在任何时候使用 session session_start() 应该是任何打印函数之前的第一行.
if you use session at any point session_start() should be the first line of just before any printing functions.
在页面开头启动会话,然后尝试打印 echo " $_SESSION["s"]";
start session at the starting of the page then try to print echo " $_SESSION["s"]";
这篇关于PHP 会话不适用于游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文