简体   繁体   English

为什么此会话变量不起作用...?

[英]Why won't this session variable work…?

I am making a game for class and when the user completes a level they proceed to the next. 我正在为课堂制作游戏,当用户完成一个关卡时,他们将继续下一个游戏。 But as they can't be allowed to access the URL directly I have tried to create a PHP session variable in the JavaScript If statement for winning the game, like this: 但是由于不允许他们直接访问URL,我尝试在JavaScript If语句中创建一个PHP会话变量来赢得比赛,如下所示:

if (passOnLevel == 0 && passengerCarrying == 0) {
    alert("You Win!");
<?php $_SESSION['levelCompleteC'] = 'levelOneComplete'; ?>
    window.location.href='gameL2.php';
}

In the gameL2.php file (level 2) I have this If statement at the top of my page: gameL2.php文件(第2级)中,我的页面顶部具有以下If语句:

<?php session_start(); ?>
<?php if (isset($_SESSION['RobotName'], $_SESSION['traction'], $_SESSION['passenger'], $_SESSION['levelCompleteC'])) {
....
....
........

However the $_SESSION variable for levelOneC to check whether the successfully completed the level seems to automatically set itself when level one loads so it doesn't actually check to see if they won the level. 但是,用于levelOneC$_SESSION变量检查是否成功完成了该级别,似乎在一级加载时自动设置了自身,因此它实际上不检查它们是否赢得了该级别。

Is there another way of doing this? 还有另一种方法吗?

PHP is rendered BEFORE the page loads, which means that you can't come along and insert PHP code to be executed after the page loaded. PHP是在页面加载之前呈现的,这意味着您无法随即插入要在页面加载之后执行的PHP代码。 Use AJAX to do your desired operation. 使用AJAX进行所需的操作。

For easy refrence, Google "XMLHttpRequest" or see here . 为了方便阅读,请访问Google“ XMLHttpRequest”或在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM