简体   繁体   English

PHP会话变量没有停留

[英]PHP Session variables not staying

So, I have this RPG game I've been working on using PHP for the hell of it. 所以,我有这个RPG游戏,我一直在努力使用PHP来实现它。 I've been trying to get some sessions to work for quite some time to save my variables over various pages, which, obviously, is what they're intended for. 我一直试图让一些会话工作很长一段时间,以便将我的变量保存在各个页面上,这显然是他们的目的。 Here's the code related to my problem from my page: 这是我的页面中与我的问题相关的代码:

Line 1: <?php session_start(); ?> 第1行: <?php session_start(); ?> <?php session_start(); ?>

This part is just determining the results from the form the user fills out to "create their character" 这部分只是确定用户填写表格“创造他们的角色”的结果

if ($_GET["race"]=="Human"){
$_SESSION["race"] = "Human";
$_SESSION["raceadj"] = "Human";
$_SESSION['racestrengthbonus'] = 0;
$_SESSION['raceendurancebonus'] = 0;
$_SESSION['raceintellectbonus'] = 0;
$_SESSION['racewillpowerbonus'] = 0;
$_SESSION['raceluckbonus'] = 0;

and for the class 而对于班级

if ($_GET["career"]=="Knight"){
$_SESSION["career"] = "Knight";
$_SESSION['classstrengthbonus'] = 10;
$_SESSION['classendurancebonus'] = 10;
$_SESSION['classintellectbonus'] = 0;
$_SESSION['classwillpowerbonus'] = 0;
$_SESSION['classluckbonus'] = 0;
$_SESSION['RightHand'] = "Iron Sword";
$_SESSION['LeftHand'] = "Wooden Shield";
$_SESSION['Armor'] = "Iron Armor";

Now here's the code where all of that is being used. 现在这里是所有这些都被使用的代码。

<?PHP
echo "<div class=\"playerstats\" align=\"center\"><span class=\"playername\">".$_GET["name"]."</span><br><span class=\"redtext\">".$_SESSION["race"]." - ".$_SESSION['career']." - ".$_SESSION["health"]." HP - ".$_SESSION["gold"]." Gold</div>
<div style=\"float:left\"><span class=\"redtext\">Strength: </span>".$_SESSION["strength"]."<br>
<span class=\"redtext\">Endurance: </span>".$_SESSION["endurance"]."<br>
<span class=\"redtext\">Intellect: </span>".$_SESSION["intellect"]."<br>
<span class=\"redtext\">Willpower: </span>".$_SESSION["willpower"]."<br>
<span class=\"redtext\">Luck: </span>".$_SESSION["luck"]."<br></div>";

echo "<div class=\"equipment\"><span class=\"hover right\">".$_SESSION["RightHand"]."</span><span class=\"redtext\"> - Right Hand</span><br>
<span class=\"hover left\">".$_SESSION['LeftHand']."</span><span class=\"redtext\"> - Left Hand</span><br>
<span class=\"hover\">".$_SESSION['Armor']."</span><span class=\"redtext\"> - Armor</span><br></div>"; ?>

When I first load up this page, everything displays correctly. 当我第一次加载此页面时,一切都正确显示。 The name, race, etc all displays what the user put in their form, but if the user uses any of the actions on the page (which are simply forms that display the results on this same page) then it doesn't save the variables and goes right to the "else" results of both race and class, with no name saved. 名称,种族等都显示用户在其表单中放置的内容,但如果用户使用页面上的任何操作(这些只是在同一页面上显示结果的表单),则它不会保存变量并且直接进入种族和阶级的“其他”结果,没有保存名称。 I'm not sure what I'm doing wrong. 我不确定我做错了什么。 Any help? 有帮助吗?

Make sure you have session_start at the beginning and in every page you wanted to use your session vars. 确保在开头和每个要使用会话变量的页面中都有session_start If you forget to declare session_start at the beginning, the session is forgotten in limbo. 如果您忘记在开头声明session_start,则会忘记会话。

EDIT: 编辑:

page1.php page1.php中

<?php session_start(); $_SESSION['bar'] = "SAVED SESSION"; ?>

page2.php 使page2.php

<?php session_start(); echo $_SESSION['bar']; ?> // outputs 'SAVED SESSION'

page3.php page3.php

<?php echo $_SESSION['bar']; ?> // outputs ''; session is forgotten

EDIT: I recreated part of your test game here: http://www.huinda.com/test/ sessions do work, you only handle it incorrectly, so they are either erased accidentally or being set to null accidentally. 编辑:我在这里重新创建了你的测试游戏的一部分: http//www.huinda.com/test/ sessions do work,你只是错误地处理它,所以它们要么被意外删除,要么被意外地设置为null。

表格提交后,您确定在$ _GET中有“竞赛”和“职业”吗?

Perhaps for form method=POST and so $_GET[var] will not find variable. 也许对于form method = POST,所以$ _GET [var]将找不到变量。 Although you said first view it appears correctly so seems the values were retrieved. 虽然您说第一个视图它看起来正确所以似乎检索了值。

I suspect you need to configure your session store in php.ini file and restart your web server. 我怀疑您需要在php.ini文件中配置会话存储并重新启动Web服务器。

session.save_path 的session.save_path

Make sure it's a writeable directory by the web server. 确保它是Web服务器的可写目录。

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

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