【发布时间】:2012-06-20 20:11:27
【问题描述】:
我想使用这样的验证码生成器:
1)在 $_SESSION 变量中保存一些安全文本 2)显示captha图像。
<img src="http://www.website.ro/captcha/captcha_source.php">
验证码图像是一个 php 文件,它读取 $_SESSION["security_text"] 并通过将 it-s 标头设置为图像来生成图像:
header("Content-type:image/jpeg");
header("Content-Disposition:inline ; filename=secure.jpg");
3)将提交的文本与存储在_SESSION中的文本进行比较
问题: -我设置 $_SESSION["outside"]="outside";在图片标签之前,但在 captcha_source.php 中 $_SESSION 变量为空。
-如果我在 captcha_source.php 的开头给它 session_start(),则 session_id 与站点的其余部分相同,但 _SESSION 仍然为空。
-如果我设置 $_SESSION["inside"]="inside";在 captcha_source.php 中,当我在 captcha_source.php 之外(在 img 标签之后)读取 SESSION 时,SESSION 只包含 ["outside"]="outside"。 (并且在 captcha_source 内部,会话打印为 inside=>inside)
-如果我删除带有 img src=captcha_source.php 的行,并将 SESSION 设置为“test”并在表单中写入“test”,提交后一切正常(但我没有图像,因为它是'不包括在内)。
-如果我不将文件包含在图像标签中,而是将其包含为 include "/captcha/captcha_source.php",它将设置会话正常,但我需要图像,而不是垃圾文本。
所以会话在页面之间工作,但不知何故不在 captcha_soruce.php 内。即使 id-s 相同,会话也完全独立。
一个预感是问题出在htaccess(但相同的会话id-s很奇怪),可能来自以下几行:(验证码文件夹的处理方式不同,但基地址应该不变)
RewriteCond $1 !^(index_ro|imagini|extra|fisiere|slider|tinymce|captcha)
RewriteRule ^(.*)/ index_ro.php?$1/
也许相同的会话与我读取文件的方式有关:从 captcha_source.php 中删除标题并打开文件 www.site.ro/captcha/captcah_source.php 使用相同的浏览器(firefox)。我看到了我打印的垃圾文本、会话 ID 和会话变量。使用同一个站点打开多个标签,保持相同的 id。
我希望它不会太长,但我已经 2 天没有遇到这个问题了。如果不行,我会用sql来做,但是我想知道问题出在哪里,这样在其他情况下就不会再出现了。
谢谢你:)
这是一个剥离代码来显示发生了什么:
//the form part
<?php
session_start();
echo session_id(); //prints the same as on all pages
//the form was not submittted
if(!$_POST["mail_form_captcha"])
{
unset($_SESSION); //just to be sure nothing remains from older sessions
//generate form that has a field "mail_form_captcha"
[...]
//generate a random text for captcha and put it in security_text
InitCaptcha();
$_SESSION["before"]="before";
?>
<img src="<?php echo "./captcha/image.php"; ?>" />
<?php
//include "./captcha/image.php"; //if uncoment this line, everything works, but the image is included as garbage text
$_SESSION["after"]="after";
print_r($_SESSION); //this prints [security_text] => 10 [before] => before [after] => after
}
else //interpret the submission
{
print_r($_SESSION); //this is empty if session_start() is at the beginning of captcha_source.php, otherwise only contains before after and security_text
}
?>
//the captcha part
<?php
session_start(); //if included, it erases the session from the other files, otherwise it leaves it intact :-/
$_SESSION["inside"]="inside";
print_r($_SESSION); //this prints [inside] => inside
echo session_id(); //this prints the same session id as on the rest of the pages
[...]
imagettftext([...] $_SESSION["security_text"]); //this draws a blank image
[...]
header("Content-type:image/jpeg");
header("Content-Disposition:inline ; filename=secure.jpg");
imagejpeg($img);
?>
【问题讨论】:
-
你打电话给
session_start了吗?您是否使用自定义会话 ID 或会话名称? -
您能否发布 captcha_soruce.php 的相关部分(即 session_start 导致 $_SESSION 调用),因为中间可能有一些东西搞砸了。
-
@netcoder 是的,我确实在每个页面的顶部和 captcha_source.php 中调用了 session_start。但我没有使用自定义会话 ID 或名称。
-
@Robbie 我正试图弄清楚如何在这个小盒子中添加代码,因为我的字符用完了
-
您未设置 ($_SESSION) - 您确定没有调用该行吗?在那个时候做一个回声来检查。