2011-05-07 56 views
0

我正在嘗試創建驗證碼,但正在創建的代碼未存儲在會話變量中。這裏是我用來創建代碼並存儲值的文件。我在我的php頁面上有一個session_start(),它也會顯示驗證碼。存儲驗證碼的會話值

<?php session_start(); 
class CaptchaSecurityImages { 

var $font = './monofont.ttf'; 

function generateCode($characters) { 
    /* list all possible characters, similar looking characters and vowels have been removed */ 
    $possible = '23456789bcdfghjkmnpqrstvwxyz'; 
    $code = ''; 
    $i = 0; 
    while ($i < $characters) { 
     $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); 
     $i++; 
    } 
    return $code; 
} 

function CaptchaSecurityImages($width='120',$height='40',$characters='6') { 
    $code = $this->generateCode($characters); 
    /* font size will be 75% of the image height */ 
    $_SESSION['security_code'] = $code; 
    $font_size = $height * 0.75; 
    $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); 
    /* set the colours */ 
    $background_color = imagecolorallocate($image, 255, 255, 255); 
    $text_color = imagecolorallocate($image, 20, 40, 100); 
    $noise_color = imagecolorallocate($image, 100, 120, 180); 
    /* generate random dots in background */ 
    for($i=0; $i<($width*$height)/3; $i++) { 
     imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); 
    } 
    /* generate random lines in background */ 
    for($i=0; $i<($width*$height)/150; $i++) { 
     imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); 
    } 
    /* create textbox and add text */ 
    $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); 
    $x = ($width - $textbox[4])/2; 
    $y = ($height - $textbox[5])/2; 
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function'); 
    /* output captcha image to browser */ 
    header('Content-Type: image/jpeg'); 
    imagejpeg($image); 
    imagedestroy($image); 
} 

} 

$width = isset($_GET['width']) ? $_GET['width'] : '120'; 
$height = isset($_GET['height']) ? $_GET['height'] : '40'; 
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6'; 

$captcha = new CaptchaSecurityImages($width,$height,$characters); 

?> 

我可以在會話中存儲其他項目,他們工作得很好。我不確定爲什麼這個值不會存儲。我發現有關獲取captcha工作的其他信息,但沒有提到在會話中存儲值的問題。有什麼建議麼?

感謝 羅伯特

+1

如果這是用於生產代碼,您是否看過[reCAPTCHA](http://www.google.com/recaptcha)? – Austin 2011-05-07 19:32:09

+1

我已經遇到了與我的網站之一相同的問題,奇怪的是我有它的工作約10個網站,這一個不保存會話..你可以評論輸出線(頭+圖片+ imagedestroy)和print_r($ _ SESSION)來查看運行文件時是否存儲它。 – 2011-05-07 19:32:52

+0

謝謝奧斯汀,我會研究一下。這就是我跑進Pendo的原因,我不確定這是否與爲圖像值設置標題有關,或者發生了什麼,但似乎有些奇怪,它可以在某些地方使用,但不是這個。 – Robert 2011-05-07 19:35:04

回答

2

我確實最終使用reCAPTCHA來完成這項任務。

0

如何被加載到用戶的瀏覽器你的形象?它在同一個域上嗎?如果不是,最可能的問題是您的Cookie被排除,因爲它是「第三方Cookie」。爲了讓瀏覽器保存這個cookie,你必須設置一個P3P頭,所以瀏覽器知道可以保存cookie。

有效的P3P頭的一個例子是:

header('P3P: CP="CAO PSA OUR"'); 

希望這有助於。

+0

圖像被加載到同一個域。它是從包含文件加載的,會對使用會話有什麼影響? – Robert 2011-05-07 19:50:27

+0

這應該沒有任何作用。問題似乎是,當請求圖像時,客戶端不會發送當前的PHP會話ID。你在文檔的頂部有'session_start();'輸出HTML來加載圖像,是否正確? – 2011-05-07 19:52:39

+0

yes session_start()位於創建圖像的頁面的頂部。 – Robert 2011-05-07 20:05:34