_SESSION在验证码文件中不可见 [英] _SESSION not visible inside captcha file
问题描述
你好,
我想使用验证码生成器,女巫的工作方式如下:
1)在_SESSION变量中保存一些序列文本
2)显示captha图片.
Hi there,
I want to use a captcha generator witch works like this:
1)Save some sequrity text in the _SESSION variable
2)Display the captha image.
<img src="http://www.website.ro/captcha/captcha_source.php">
验证码图像是一个php文件,该文件读取$ _SESSION ["security_text"]并通过将其标题设置为图像来生成图像:
the captcha image is a php file which reads the $_SESSION["security_text"] and generates an image, by setting it-s header to an image:
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",则提交后一切正常(但是我没有图像,因为它不是包括).
因此,会话可以逐页进行,但不可以在captcha_soruce.php中进行.即使id是相同的,会话也要完全独立.
如果不是将文件包含在image标记内,而是将其包含为include/captcha/captcha_source.php,它将会话设置为可以,但是我需要的是图像,而不是垃圾文本.
一种预感是问题出在htaccess上(但是相同的会话ID却很奇怪),也许是从以下几行得出的:(验证码文件夹被不同地对待,但是基址应该保持不变)
3)compare the submited text to the one stored inside _SESSION
The problem:
-I set $_SESSION["outside"]="outside"; before the image tag, but inside captcha_source.php the $_SESSION variable is empty.
-If I give it session_start() at the beginning of captcha_source.php, the session_id IS THE SAME as for the rest of the site, but _SESSION is still empty.
-If I set $_SESSION["inside"]="inside"; inside captcha_source.php, when I read SESSION outside of captcha_source.php (after the img tag), SESSION only includes ["outside"]="outside". (And inside captcha_source, session prints as inside=>inside)
-If I remove the line with img src=captcha_source.php, and set SESSION to "test" and write "test" in the form, everything works after submitting (but I don''t have the image, because it wasn''t included).
So session works from page to page, but somehow not inside captcha_soruce.php. Even thow the id-s are the same, the sessions seam to be totally independent.
If instead of including the file inside the image tag, I include it as include "/captcha/captcha_source.php" it sets the sessions ok, but I need the image, not garbage text.
One hunch is that the problem is from htaccess (but the identical session id-s are strange), maybe from these lines: (the captcha folder is treated differently, but the base address should be unchanged)
RewriteCond $1 !^(index_ro|imagini|extra|fisiere|slider|tinymce|captcha)
RewriteRule ^(.*)/ index_ro.php?$1/
也许相同的会话与我读取文件的方式有关:从captcha_source.php删除标头,然后使用相同的浏览器(firefox)打开文件www.site.ro/captcha/captcah_source.php.我看到了我打印的垃圾文本,会话ID和会话变量.使用相同的站点打开多个选项卡,保留相同的ID.
我希望这不会太久,但是距离我解决这个问题已经过去了2天了.如果它不起作用,我将使用sql进行此操作,但我想知道问题出在哪里,因此在其他情况下也不会再次出现.
谢谢:)
这是一个剥离的代码,可以更清楚地显示发生的情况:
Maybe the identical sessions have to do with the way I read the files: remove the header from captcha_source.php and open the file www.site.ro/captcha/captcah_source.php whitch the same browser (firefox). And I see the garbage text and session id and session variables whitch I printed. Openning multiple tabs with the same site, keeps the same id.
I hope it''s not to long, but it''s been 2 days since I strougled with this problem. If it won''t work, I''ll do this with sql, but I would like to know where the problem is so it won''t show up again in other circumstances.
Thank you :)
Here is a stripped code to show more clearly what happens:
//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);
?>
好的,自从我被困在这里以来已经有好几天了.我将文本保存在SQL表中,然后从那里读取它.如果有人知道是什么原因引起的,我很感兴趣,所以我不会再陷入这个问题了:)
Ok, it''s been to many days since i''ve been stuck here. I''ll save the text in a SQL table and read it from there. If anyone knows what''s causing this, I''m interested, so I won''t fall in this problem again :)
推荐答案
_SESSION ["security_text ]并通过将其标题设置为图像来生成图像:
_SESSION["security_text"] and generates an image, by setting it-s header to an image:
header("Content-type:image/jpeg");
header("Content-Disposition:inline ; filename=secure.jpg");
3)将提交的文本与_SESSION中存储的文本进行比较
问题:
-我设置了
3)compare the submited text to the one stored inside _SESSION
The problem:
-I set
_SESSION ["outside"] ="outside";在图片标签之前,但在captcha_source.php中,
_SESSION["outside"]="outside"; before the image tag, but inside captcha_source.php the
_SESSION变量为空.
-如果我在captcha_source.php的开头给它session_start(),则session_id与网站的其余部分相同,但是_SESSION仍然为空.
-如果设置了
_SESSION variable is empty.
-If I give it session_start() at the beginning of captcha_source.php, the session_id IS THE SAME as for the rest of the site, but _SESSION is still empty.
-If I set
这篇关于_SESSION在验证码文件中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!