<?php
$mysql_handle = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Error connecting to database server");
mysql_select_db($dbname, $mysql_handle)
or die("Error selecting database: $dbname");
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<center>
<h2>Sorry, it looks like you lost...</h2>
<h3>Your score was: <?php echo $_SESSION["score"] ?>! If this was your highest score, it has been saved.</h3>
<button id="restartButton" type="button" onclick="restart()" name="restart" class="btn btn-info">Restart</button>
</center>
</body>
</html>
<script>
//when restart is clicked
function restart()
{
header("http://web.engr.oregonstate.edu/~chathamb/CodingPractice/ImprovedPongGame/game.php");
}
</script>
<?php
mysql_close($mysql_handle);
?>
这是我在gameOver.php中的代码,我似乎无法弄清楚为什么我的$ _SESSION [“得分”]没有得到正确维护。我第一次通过测试,它正确显示4,现在我有一些问题甚至打印出回声,并且在执行标题重定向的restart()函数后它不会重置。
从我的研究中,我认为问题围绕header()重定向,但我似乎无法弄明白。我尝试了一个AJAX解决方法,但它没有用。
是的,session_start();是我的game.php代码的代码顶部。 game.php代码非常冗长而且有点混乱所以我会免除你看到它的痛苦。