2016-08-22 101 views
-4

我在創建包含驗證碼的PHP圖像時遇到問題。基本上它過去運行良好,但現在當我打開頁面時,突然看到圖像不是第一次出現。這實際上是第一次發生。下面是index.php文件,其中包含HTML代碼:PHP不會生成驗證碼圖像

<div class="wrapper"> 
    <form name="counter"><input type="text" size="8" name="d2"></form> 
     <form class="login" method="POST" action=""> 
      <p class="title">Are you human?</p> 
      <div id="captchaimage"> 
       <img src="generate.php"> 
      </div> 
      <input type="text" name="scr" size="6" placeholder="Write what you see here" autofocus/> 
      <a href="http://zite.pouyavagefi.com/helpcenter.php">Can not read the code?</a> 
      <input class="submititon" type="submit" value="Submit" name="submit"></input> 
    </form> 
    <footer><a target="_blank" href="http://www.zite.pouyavagefi.com">Zite v2.0</a></footer> 
</div> 

這是generate.php文件創建驗證碼:

<?php 
session_start(); 
header('Content-type: image/jpeg'); 

$text = $_SESSION['secure']; 
$font_size = 26; 
$image_width = 100; 
$image_height = 40; 

$image = imagecreate($image_width, $image_height); 
imagecolorallocate($image,255,255,255); 
$text_color = imagecolorallocate($image,0,0,0); 

for($x=1;$x<=30;$x++){ 
    $x1 = rand(1, 100); 
    $y1 = rand(1, 100); 
    $x2 = rand(1, 100); 
    $y2 = rand(1, 100); 

    imageline($image, $x1, $y1 ,$x2 ,$y2, $text_color); 
} 

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text); 
imagejpeg($image); 
?> 

這我的網頁的打印屏幕:

print screen

說真的,我不是在做什麼,因爲一切都看起來很完美,它曾經正常工作!所以如果你知道什麼是問題,請讓我知道。由於

+0

如果您直接在瀏覽器中請求'generate.php'文件,會發生什麼?它顯示什麼? – richsage

+0

當我打開generate.php時,圖像不顯示。但不會出現錯誤,並且錯誤打印已打開。 – Pouya

+0

刪除'header('Content-type:image/jpeg');'查看錯誤,如果有的話。尤其是檢查會話變量! –

回答

0

你的問題是在這裏:

<?php 
session_start(); 
header('Content-type: image/jpeg'); 

這種差距的<?php之前是導致session_start拋出一個錯誤。您需要沒有空格或其他瀏覽器輸出session_start(和其他標題)功能之前。

您也有效地將4/5個空格作爲圖像文件數據流的開頭,這顯然會導致任何圖像無法正確顯示。