2012-06-18 35 views
2

我想用驗證碼發生器女巫是這樣工作的:_SESSION不可見裏面的captcha文件

1)除一些安全文字$ _SESSION變量 2)顯示captha形象。

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

人機識別圖像是一個PHP文件,該文件讀取$ _SESSION [ 「security_text」],生成圖像,通過將其設定-S包頭加到一個圖像:

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

3)比較submited text to the one stored inside _SESSION

問題: -I set $ _SESSION [「outside」] =「outside」;在圖片標籤之前,但在captcha_source.php中,$ _SESSION變量是空的。

- 如果我在captcha_source.php的開頭給它session_start(),session_id與網站的其餘部分相同,但是_SESSION仍然是空的。

- 如果我設置$ _SESSION [「裏面」] =「裏面」;在captcha_source.php裏面,當我讀取captcha_source.php之外的SESSION(在img標籤之後)時,SESSION只包含[「outside」] =「outside」。 (並在裏面captcha_source,會話打印爲裏面=>裏面)

- 如果我刪除img src = captcha_source.php行,並設置SESSION爲「測試」並在表單中寫入「測試」,一切工作後提交(但我沒有圖像,因爲它沒有包括在內)。

- 如果不是將文件包含在image標籤中,而是將其包含爲包含「/captcha/captcha_source.php」,它會設置會話正常,但我需要圖像而不是垃圾文本。

因此,會話在頁面之間工作,但不知何故在captcha_soruce.php裏面。即使這些身份證是相同的,會議縫合完全獨立。

一個直覺是,這個問題是來自htaccess的(但相同的會話ID-S很奇怪),也許從這些線:(驗證碼文件夾被區別對待,但基址應是不變的)

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以及會話變量。使用相同的網站打開多個標籤,保持相同的標識。

我希望不會太久,但是我一直在解決這個問題已經過去了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); 
?> 
+0

你叫'session_start'嗎?你使用自定義會話ID還是會話名稱? – netcoder

+0

您可以發佈captcha_soruce.php的相關部分(即session_start導致$ _SESSION調用),因爲這可能是中間弄亂了東西。 – Robbie

+0

@netcoder是的,我確實在每個頁面的頂部和captcha_source.php中調用了session_start。但我沒有使用自定義會話ID或名稱。 –

回答

0

正如我說我不會去嘗試別的anythind這個驗證碼,我試圖將所有的文件調用它的文件在同一個目錄中(它在一個文件./captcha中),它現在可以工作了!它在不同的目錄中有什麼問題?

+0

今天我遇到了這個問題,由於它現在正在工作。你知道爲什麼會發生嗎? @MihaiZaharescu –