2011-12-27 85 views
-1

我搜索了这个网站上的类似问题,并且尝试了十多个建议,但只有一点点接近解析。会话变量为空

我开始使用HTML & PHP,所以这是一个非常简单的几个脚本。

我正在设置一个数学问题的数组(测试我9岁的儿子)。 第一个脚本“mathtest.php”设置数组并在$ _session全局变量中设置了一些变量,然后表单将问题的答案提交给“mathtest1.php”。

当我到达“mathtest1.php”时,My $ _session变量丢失。

请帮忙。我知道我可以用饼干做点什么,但我真的想提高我对会话的理解。

这里的2个脚本:

“mathtest.php”:

<?php 
session_start(); 
?> 
<html> 
<title>Math Test</title> 
<head>Math Test</head> 
<body> 
<?php 
$arrayindex = 0; 

for ($L = 1; $L <= 12; $L++) { 
    for ($R = 12; $R >= 1; $R--) { 
     $setupquestions[$arrayindex] = $L.'*'.$R; 
     $arrayindex++; 
    } 
} 

$_session["questions"] = $setupquestions; 
$_session["randomkey"] = array_rand($_session["questions"],1); 

?> 

<form action="mathtest1.php" method="post"> 
    What is <?php echo $_session["questions"][$_session["randomkey"]]." ?" ?> 
    <input type="text" name="answer"> 
    <input type="submit" name = "submit"> 
</form> 

</body> 
</html> 

如预期,但下面的脚本都是空值,会话变量我“米试图访问上述作品脚本以及使用

“mathtest1.php”:

<?php 
session_start(); 
?> 
<html> 
<body> 

<?php 

if(isset($_POST['submit'])) 
{ 
    $answer = $_POST['answer']; 
    $result = eval("return $_session[questions]$_session[randomkey];"); 
    echo "result = ".$result."<br />"; 
    if ($answer == $result) { 
     echo "Correct!!"; 
    } 
    else { 
     echo "WRONG!!"; 
    } 
} 

$_session["randomkey"] = array_rand($_session["questions"],1); 

?> 
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    What is <?php echo $_session["questions"][$_session["randomkey"]]." ?" ?> 
    <input type="text" name="answer"> 
    <input type="submit" name = "submit"> 
</form> 


</body> 

</html> 

其他详情: OS X,Chrome浏览器,最新版本的PHP XAMPP安装&脚本与XAMPP位于同一台笔记本电脑上,而不是位于外部服务器上。 会话cookie为ON ... Trans_ID为ON 我已阅读&写入会话保存路径。

回答

4

$_SESSION应该是大写。

+0

Savid打我! – Virendra 2011-12-27 01:13:47

+0

认真吗? .... 有用! ...我想我已经了解到编程是非常直接的。非常感谢。 – Loumont 2011-12-27 03:13:17

+0

@Loumont,因为这回答了你的问题,你应该用这个答案左边的复选标记来标记它。 – jlafay 2011-12-27 17:26:29

0

不像function names ...

function bar(){ 
} 

function Bar(){ 
} 

...

Fatal error: Cannot redeclare Bar() (previously declared in C:\tmp\test.php:3) in C:\tmp\test.php on line 7 

... variable names是区分大小写的PHP:

$foo = 1; 
$Foo = 2; 
$FOO = 3; 
var_dump($foo, $Foo, $FOO); 

...

int(1) 
int(2) 
int(3) 

这也适用于predefined variables,包括$_SESSION