简体   繁体   English

PHP - 无法设置会话变量

[英]PHP - session variable cannot be set

I wrote a page 'captcha.php' to generate a math captcha and send the result through session to the page which request it. 我写了一个页面'captcha.php'来生成数学验证码,并通过会话将结果发送到请求它的页面。
In captcha.php: 在captcha.php中:

$_SESSION['captcha'] = $var;//$var is the calculate result

In index.php: 在index.php中:

<img src="/captcha.php"/><input id="captchaa" type="text" name="a"/>
...
<?php    
if($_POST['a']==$_SESSION['captcha'])
...

But I got 'Undefined index: captcha' error. 但是我得到了“Undefined index:captcha”错误。
Any hint? 任何提示? Or what else more information do you need? 或者您还需要更多其他信息?

UPDATE: these lines are in both files if(!isset($_SESSION)) { session_start(); 更新:如果(!isset($ _ SESSION)){session_start();这些行都在两个文件中。 } }

UPDATE: I think i found the reason. 更新:我想我找到了原因。 My index.php is in the frame of Yii, and it has a session with id, but the captcha.php is not within the framework, so they cannot share a session.I tried to make it a view(/validation/captcha ),but it won't generate image properly that way, don't know why. 我的index.php在Yii的框架中,它有一个带id的会话,但是captcha.php不在框架内,所以他们不能共享一个session.I试图让它成为一个视图(/ validation / captcha) ,但它不会以这种方式正确生成图像,不知道为什么。

Now the problem is how to use Yii's session in captcha.php. 现在的问题是如何在captcha.php中使用Yii的会话。

你在两个文件中都使用了session_start函数吗?

很难说没有看到captcha.php的其余部分,你在设置会话变量之前调用了session_start()吗?

The variable $_SESSION['captcha'] will not be available in index.php until the next page load as it is being created in a request after the one you are currently in, if that make sense. 变量$ _SESSION ['captcha']在index.php中将不可用,直到下一页加载,因为它是在您当前所在的请求之后创建的,如果有意义的话。

Checking if the form has been submitted /then/ checking the value against the $_SESSION should work as long as session_start() is being used correctly. 检查表单是否已提交/然后/只要正确使用session_start(),就可以检查$ _SESSION的值。

I agree with mishu about session_start. 我同意mishu关于session_start。 'Undefined index: captcha' is not an error. '未定义索引:验证码'不是错误。 It's rather a warning. 这是一个警告。 If you don't suppress the output of warning messages, you should check if specified array index really exists: 如果不抑制警告消息的输出,则应检查指定的数组索引是否确实存在:

if(isset($_POST['a']) && isset($_SESSION['captcha'])&&$_POST['a']==$_SESSION['captcha'])
...

to suppress warnings output you should use error_reportning function, if it's not critical to you. 要禁止警告输出,您应该使用error_reportning函数,如果它对您不重要。

It's because captcha.php is processed in a separate request by browser . 这是因为captcha.php是由浏览器在单独的请求中处理的。 So, the flow is the following: 所以,流程如下:

  1. PHP executes index.php file. PHP执行index.php文件。 NO $_SESSION['captcha'] is set, so nothing is outputted NO $_SESSION['captcha']已设置,因此不输出任何内容
  2. PHP flushes the content to the browser. PHP将内容刷新到浏览器。
  3. Browser sees src parameter of the img and loades that url 浏览器看到src的参数img和loades该网址
  4. PHP executes captcha.php and sets $_SESSION['captcha'] , but the page is already rendered. PHP执行captcha.php并设置$_SESSION['captcha'] ,但页面已经呈现。

So, actually your $_SESSION['captcha'] is set after the index.php is processed and even after another HTTP request. 所以,实际上你的$_SESSION['captcha']处理index.php 之后设置的,甚至是在另一个HTTP请求之后。

As a workaround you may use AJAX loading captcha, passing both captcha value and image via AJAX request and than injecting them into HTML page. 作为一种解决方法,您可以使用AJAX加载验证码,通过AJAX请求传递验证码值和图像,而不是将它们注入HTML页面。

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

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