2013-01-05 35 views
2

我有這樣的代碼:PHP驗證碼會話一點兒也不更新

captcha.php:

session_start(); 
class Captcha { 
    protected $code; 
    protected $width = 35; 
    protected $height = 150; 

    function __construct() { 
     $this->code = substr(sha1(mt_rand()), 17, 6); 
     $_SESSION['captcha'] = $this->code; 
    } 

    function getCode(){ 
     return $this->code; 
    } 

    function showImage() { 
      // here comes the code that builds the image. 
      // it works fine! 

    } 
} 


$image = new Captcha(); 

$image->showImage(); 

而在我的登錄表單我有:

<iframe src="includes/captcha.php" frameborder="0" height="65" width="180"></iframe> 

如果我print_r($_SESSION)$_SESSION['captcha']總是在延遲:它包含前面的captcha c頌歌,而不是正在顯示的電流。

我該怎麼辦?

回答

2
<iframe src="includes/captcha.php" frameborder="0" height="65" width="180"></iframe> 

應該是:

<img src='includes/captcha.php' style='height:65px;width:180px' /> 

正如你應該加載圖像作爲圖像而不是一個iframe。

如果在父頁面上打印出來,您的CAPTCHA代碼將始終爲舊值,因爲在加載主頁面後發生的captcha.php僅寫入新值,因此新會話值不可用那時候。所以一切正常工作正常。

+0

謝謝,你是如此吧! – user1936192

0

所以你必須這樣做,是因爲瀏覽器的緩存:

<iframe src="includes/captcha.php?<?php echo microtime();?>" 
frameborder="0" height="65" width="180"></iframe>