_SESSION在验证码文件中不可见

时间:2012-06-18 19:45:42

标签: php session captcha

我想使用像这样工作的验证码生成器:

1)在$ _SESSION变量中保存一些安全文本 2)显示captha图像。

<img src="http://www.website.ro/captcha/captcha_source.php">

验证码图像是一个php文件,它读取$ _SESSION [“security_text”]并通过将其标题设置为图像来生成图像:

header(&quot;Content-type:image/jpeg&quot;);
header(&quot;Content-Disposition:inline ; filename=secure.jpg&quot;);

3)将提交的文本与存储在_SESSION

中的文本进行比较

问题: - 我设置$ _SESSION [“outside”] =“outside”;在image标签之前,但在captcha_source.php内部,$ _SESSION变量为空。

- 如果我在captcha_source.php的开头给它session_start(),则session_id与网站的其余部分相同,但_SESSION仍为空。

- 如果我设置$ _SESSION [“inside”] =“inside”;在captcha_source.php里面,当我在captcha_source.php之外读取SESSION(在img标签之后)时,SESSION只包含[“outside”] =“outside”。 (在captcha_source中,会话打印为inside =&gt; inside)

- 如果我用img src = captcha_source.php删除该行,并将SESSION设置为“test”并在表单中写入“test”,则一切都在提交后有效(但我没有图像,因为它不是不包括在内。

- 如果不是将文件包含在图像标记内,我将其包含为包含“/captcha/captcha_source.php”,它将会话设置为ok,但我需要图像,而不是垃圾文本。

因此会话在页面之间起作用,但不知何故不在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执行此操作,但我想知道问题所在,以便在其他情况下不再显示。

谢谢:)

这是一个剥离的代码来显示hapens:

//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);
?>

1 个答案:

答案 0 :(得分:0)

就像我说我不会尝试使用此验证码的其他任何东西,我尝试将所有文​​件移动到与调用它的文件相同的目录中(它在文件./captcha中)并且它现在可以正常工作!它在不同的目录中有什么问题?