【问题标题】:PHP | Captcha won't write to $_SESSIONPHP |验证码不会写入 $_SESSION
【发布时间】:2013-06-02 18:04:55
【问题描述】:

我正在构建一个表单 + 表单验证类,我还想在其中添加验证码。 验证码图像正在显示,但它没有在 $_SESSION 中存储任何内容。

我正在使用这个验证码脚本:

https://github.com/gesf/captcha.class.php

现在在我的控制器中我使用这个:

$data['regform']->addfield('user_captcha', 'Human verification', 'captcha', 'captcha' );

这会产生以下内容:

<label>
<span>Human verification</span>
<img name="user_captcha" src="http://www.websiteurl.com/dev/misc/captcha.php?c=1"><input type="text" name="user_captcha" value="" />
</label>

图像显示正常。但是我无法验证输入,因为它没有写入会话。现在在图像文件 captcha.php 中,它加载了 Captcha 类,并在这个类构造函数中尝试写入会话:

function Captcha($letter = '', $case = 5) {

$this->_capCase = $case;

if (empty($letter)) {
    $this->StringGen();
} else {
    $this->_capLength = strlen($letter);
    $this->_capString = substr($letter, 0, $this->_capLength);
}

@session_start();
$_SESSION['asd'] = 'asd';
$_SESSION["CAPTCHA_HASH"] = sha1($this->_capString);

$this->SendHeader();
$this->MakeCaptcha();

}

我的会话总是空的。但是当我尝试以下操作时:

<?php $_SESSION['bleh'] = 'asd'?>

<?php echo $form; ?>

它将“bleh”添加到会话中,就像它应该的那样。

我真的不明白为什么它不会写入会话..

有人可以帮帮我吗?

谢谢!!

【问题讨论】:

  • 你的应用程序总是调用 session_start() 吗?
  • 是的,我的 index.php 文件包含一个 session_start

标签: php session captcha


【解决方案1】:

确保在每个页面的任何输出之前调用 session_start()。正如我所看到的,您正在使用 @ 运算符,这会关闭一些错误。你能删除它并告诉我们它输出什么吗? 此外,您的 sessiaon_start() 调用位于脚本中间的某个位置。也许在此之前还有一些其他的输出。

【讨论】:

    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    相关资源
    最近更新 更多