2012-10-21 45 views
-1

可能重複:
Does html can be use with dynamic generated images in php?無法顯示圖像,因爲它包含錯誤PHP的

我試圖生成PHP驗證碼。我相信我的代碼的權利,但我沒能獲得在browser..this圖像是代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<?php header('Content-type: image/png');?> 
<?php session_start(); 
$md5 = md5(microtime() * time()); 
$string = substr($md5, -5); 
$captcha = imagecreatefrompng("./captcha.png"); 
$black = imagecolorallocate($captcha, 0, 0, 0); 
$line = imagecolorallocate($captcha,233,239,239); 
imageline($captcha,0,0,39,29,$line); 
imageline($captcha,40,0,64,29,$line); 
$_SESSION['key'] = md5($string); 
imagestring($captcha, 5, 20, 10, $string, $black); 
imagepng($captcha);?> 
</body> 
</html> 

PNG圖像是在同一文件夾中的代碼。在GD選項在php..I啓用clueless..any幫助表示讚賞...謝謝

+0

這些輸入代碼是什麼? – GolezTrol

回答

0

您設置<?php header('Content-type: image/png');?>session_start();

你需要創建一個圖像腳本前斜面輸出任何東西,處理圖像,然後鏈接到該腳本的HTML內

例如: captcha.php

<?php 
session_start(); 
header('Content-type: image/png'); 
$md5 = md5(microtime() * time()); 
$string = substr($md5, -5); 
$captcha = imagecreatefrompng("./captcha.png"); 
$black = imagecolorallocate($captcha, 0, 0, 0); 
$line = imagecolorallocate($captcha,233,239,239); 
imageline($captcha,0,0,39,29,$line); 
imageline($captcha,40,0,64,29,$line); 
$_SESSION['key'] = md5($string); 
imagestring($captcha, 5, 20, 10, $string, $black); 
imagepng($captcha); 
?> 

你的HTML

<?php session_start(); ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<img src="captcha.php"/> 
</body> 
</html> 
0

我想這是你的CAPTCHA圖像的來源(例如,一個「image.php」文件),它從別的地方加載(例如從「captcha.php」加入<img src="image.php" />)。您也可能需要在「captcha.php」文件中包含session_start()

從您發佈的代碼中刪除所有HTML。

此外,通常,在您準備好(並且首先檢查錯誤,如果適用)之前永遠不會發送圖像內容類型。

<?php 
    session_start(); 
    $md5 = md5(microtime() * time()); 
    $string = substr($md5, -5); 
    $captcha = imagecreatefrompng("./captcha.png"); 
    $black = imagecolorallocate($captcha, 0, 0, 0); 
    $line = imagecolorallocate($captcha,233,239,239); 
    imageline($captcha,0,0,39,29,$line); 
    imageline($captcha,40,0,64,29,$line); 
    $_SESSION['key'] = md5($string); 
    imagestring($captcha, 5, 20, 10, $string, $black); 
    Header('Content-type: image/png'); 
    imagepng($captcha); 
?> 

注意:您運行的是MD5字符串的MD5。那是對的嗎?爲什麼不使用uniqid()而不是md5(microtime() * time()),並在_SESSION中存儲$md5

相關問題