我正在尝试为我的游戏制作一个简单的战斗剧本,但在战斗之后,我不希望玩家能够点击刷新并再次与怪物战斗..或者能够点击提交一遍又一遍地不断获得奖励。所以在这个人击中攻击按钮并且战斗显示结果之后我还需要做什么呢?这样做不会发生这种情况?如果我尝试session_destroy()记录播放器以及解决问题:/
这是我的代码任何帮助吗?
if(isset($_POST['Submit']))
{
$player=$_SESSION['username'];
$playerstats1="SELECT * from users where username='$player'";
$playerstats2=mysql_query($playerstats1) or die ("Could not find player");
$playerstats3=mysql_fetch_array($playerstats2);
$pokemonstat1="SELECT * from user_pokemon where belongsto='$player' AND slot='1'";
$pokemonstat2=mysql_query($pokemonstat1) or die ("Could not find pokemon");
while($row = mysql_fetch_array($pokemonstat2)){
$yourmonster="SELECT * from pokemon where name='".$row['pokemon']."'";
$yourmonster2=mysql_query($yourmonster) or die ("Cannot select battle the pokemon");
$yourmonster3=mysql_fetch_array($yourmonster2);
$monstername=$_SESSION['pokemon'];
$monstername=strip_tags($monstername);
$selmonster="SELECT * from pokemon where name='$monstername'";
$selmonster2=mysql_query($selmonster) or die ("Cannot select battle the pokemon");
$selmonster3=mysql_fetch_array($selmonster2);
$totalskill=$yourmonster3[att] * $row['level'] + $selmonster3[att] * 5;
$randomnumber=rand(1,$totalskill);
if($randomnumber<=$yourmonster3[att] * $row['level'])
{
echo "<center>";
echo "you have won!";
echo "</center>";
} else {
echo "<center>";
echo "you have lost!";
echo "</center>";
}
}
}
再次更新。
$battle_id = md5(uniqid(rand(), true));
echo $battle_id;
// $battle_id would be something like 9a8ab59df7079208843086e9b49a7862
// initialise the battle log
if(!isset($_SESSION['battle_log']) || !is_array($_SESSION['battle_log']))
{
$_SESSION['battle_log'] = array();
}
// Check if the battle hasn't been played
if(!in_array($battle_id, $_SESSION['battle_log']))
{
// add played battle to the log
// ... your battle code goes here
if(isset($_POST['Submit']))
{
$player=$_SESSION['username'];
$playerstats1="SELECT * from users where username='$player'";
$playerstats2=mysql_query($playerstats1) or die ("Could not find player");
$playerstats3=mysql_fetch_array($playerstats2);
$pokemonstat1="SELECT * from user_pokemon where belongsto='$player' AND slot='1'";
$pokemonstat2=mysql_query($pokemonstat1) or die ("Could not find pokemon");
while($row = mysql_fetch_array($pokemonstat2)){
$yourmonster="SELECT * from pokemon where name='".$row['pokemon']."'";
$yourmonster2=mysql_query($yourmonster) or die ("Cannot select battle the pokemon");
$yourmonster3=mysql_fetch_array($yourmonster2);
$monstername=$_SESSION['pokemon'];
$monstername=strip_tags($monstername);
$selmonster="SELECT * from pokemon where name='$monstername'";
$selmonster2=mysql_query($selmonster) or die ("Cannot select battle the pokemon");
$selmonster3=mysql_fetch_array($selmonster2);
$totalskill=$yourmonster3[att] * $row['level'] + $selmonster3[att] * 5;
$randomnumber=rand(1,$totalskill);
if($randomnumber<=$yourmonster3[att] * $row['level'])
{
echo "<center>";
echo "you have won!";
echo "</center>";
} else {
echo "<center>";
echo "you have lost!";
echo "</center>";
}
}
}
$_SESSION['battle_log'][] = $battle_id;
}else {
echo "Don't try to cheat...";
}
答案 0 :(得分:1)
只需在会话中设置一个值,表示用户已经玩过该战斗。然后,您可以检查该值,看看您的用户是否已经参加过这场战斗。
您在会话中保存的值将是唯一的,例如战斗ID。如果这是你没有的东西,那么你可以通过在战斗中签名所有独特的值来创建一个独特的战斗参考。像这样: -
$battle_id = md5($player.$row['pokemon'].$monstername);
// $battle_id would be something like 9a8ab59df7079208843086e9b49a7862
在您的脚本开始时,初始化所有战斗的日志: -
// initialise the battle log
if(!isset($_SESSION['battle_log']) || !is_array($_SESSION['battle_log']))
{
$_SESSION['battle_log'] = array();
}
然后在任何战斗开始之前检查它是否还没有被播放
// Check if the battle hasn't been played
if(!in_array($battle_id, $_SESSION['battle_log']))
{
// ... your battle code goes here
// add played battle to the log
$_SESSION['battle_log'][] = $battle_id;
}
所以这些方面应该有用: -
// initialise the battle log
if(!isset($_SESSION['battle_log']) || !is_array($_SESSION['battle_log']))
{
$_SESSION['battle_log'] = array();
}
if(isset($_POST['Submit']))
{
$player=$_SESSION['username'];
$playerstats1="SELECT * from users where username='$player'";
$playerstats2=mysql_query($playerstats1) or die ("Could not find player");
$playerstats3=mysql_fetch_array($playerstats2);
$pokemonstat1="SELECT * from user_pokemon where belongsto='$player' AND slot='1'";
$pokemonstat2=mysql_query($pokemonstat1) or die ("Could not find pokemon");
while($row = mysql_fetch_array($pokemonstat2))
{
$yourmonster="SELECT * from pokemon where name='".$row['pokemon']."'";
$yourmonster2=mysql_query($yourmonster) or die ("Cannot select battle the pokemon");
$yourmonster3=mysql_fetch_array($yourmonster2);
$monstername=$_SESSION['pokemon'];
$monstername=strip_tags($monstername);
$selmonster="SELECT * from pokemon where name='$monstername'";
$selmonster2=mysql_query($selmonster) or die ("Cannot select battle the pokemon");
$selmonster3=mysql_fetch_array($selmonster2);
// generate the battle id based on the unique battle details
$battle_id = md5($player.$row['pokemon'].$monstername);
$totalskill=$yourmonster3[att] * $row['level'] + $selmonster3[att] * 5;
$randomnumber=rand(1,$totalskill);
if($randomnumber<=$yourmonster3[att] * $row['level'])
{
echo "<center>you have won!</center>";
} else {
echo "<center>you have lost!</center>";
}
// Check if the battle hasn't been played
if(!in_array($battle_id, $_SESSION['battle_log']))
{
// any code below will only be run once per battle
// ...
// add played battle to the log
$_SESSION['battle_log'][] = $battle_id;
}
}
}
注意: 请注意,会话只是暂时的,因此一旦会话被销毁,所有战斗历史都会丢失,因此会保留数据。您可以创建一个battle
表来执行此操作。
答案 1 :(得分:0)
在战斗开始前生成一个特殊键并将其指定为一个会话。还可以将其作为HTML表单中的隐藏输入推送到HTML。
当用户提交表单时,请检查提交和特殊键。
如果匹配,则用户玩过一次战斗。处理战斗脚本时,请务必删除/重新生成密钥。
如果用户按F5并再次重新发布表单,则无效,因为密钥无效。他们需要刷新页面才能获得新密钥。
至于session_destroy()
,你不需要这个。只需unset
相应的会话,或为该会话提供新值。