2015-11-07 103 views
2

我是新來的PHP,我想做capthca ...我寫了代碼,驗證等,因此我得到的只是一張小圖片......而沒有任何東西它...和我的計劃不要告訴我哪裏是一個錯誤Captcha php ...我不知道什麼是錯誤

這是我在captcha.php代碼:

<?php 
session_start();`enter code here` 


    // build the base captcha image 

$img = imagecreatetruecolor(80,30); 

$white = imagecolorallocate($img, 255, 255, 255); 
$black = imagecolorallocate($img, 0, 0, 0); 
$grey = imagecolorallocate($img,150,150,150); 
$red = imagecolorallocate($img, 255, 0, 0); 
$pink = imagecolorallocate($img, 200, 0, 150); 

// build the base captcha image - END 

// building randomString 
function randomString($length){ 
    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    srand((double)microtime()*1000000); 
    $str = ""; 
    $i = 0; 

     while($i <= $length){ 
      $num = rand() % 33; 
      $tmp = substr($chars, $num, 1); 
      $str = $str . $tmp; 
      $i++; 
     } 
    return $str; 
} 

for($i=1;$i<=rand(1,5);$i++){ 
    $color = (rand(1,2) == 1) ? $pink : $red; 
    imageline($img,rand(5,70),rand(5,20), rand(5,70)+5,rand(5,20)+5, $color); 
} 
// building randomString - END 

// fill image with a white rectangle 
imagefill($img, 0, 0, $white); 

$string = randomString(rand(7,10)); 
$_SESSION['string'] = $string; 

imagettftext($img, 11, 0, 10, 20, $black, "calibri.ttf", $string); 
// fill image with a white rectangle - END 

// type of image 
header("Content-type: image/png"); 
imagepng($img); 


?> 

這是我在contact.php代碼:

<?php 

session_start(); 

if(isset($_POST['submit'])) { 

    if(!empty($_POST['ime']) && !empty($_POST['email']) && !empty($_POST['poruka']) && !empty($_POST['code'])) { 

     if($_POST['code'] == $_SESSION['rand_code']) { 

      // send email 
      $accept = "Poruka uspesno poslata."; 

     } else { 

      $error = "Pogresan kod."; 
       } 

    } else { 

     $error = "Molimo Vas popunite sva polja.";  
    } 
} 

?> 

<!DOCTYPE html > 
<html > 
<head> 
<title>Kontaktirajte nas</title> 

</head> 

<body> 

<?php if(!empty($error)) echo '<div class="error">'.$error.'</div>'; ?> 
<?php if(!empty($accept)) echo '<div class="accept">'.$accept.'</div>'; ?> 

<form method="post"> 
    <p>Ime<input type="text" name="ime" /></p> 
    <p>Email<input type="text" name="email" /></p> 
    <p>Poruka<textarea name="poruka" rows=」25」 cols=」40」></textarea></p> 
     <img src="captcha.php"/> 
    <p>Posalji<input type="text" name="code" /></p> 
    <p><input type="submit" name="submit" value="Posalji" class="button" /></p> 
    <p>"IPO" MASARIKOVA br.5, BEOGRAD</p> 
</form> 

</body> 
</html> 
+0

,做你所期望的結果呢?你有'error_reporting'打開?您是否嘗試過訪問captcha.php **,而不使用** header-changing-type來查看它是否打印出錯誤信息? – Terminus

+0

我didnt ..但我會盡力..謝謝你... –

+0

我期望得到圖像應該在哪裏寫一些隨機字母和數字組合...但我只得到破碎的圖片的圖像...如果你知道什麼我的意思是... –

回答

0

當您將標頭設置爲header("Content-type: image/png");時,頁面將不會呈現其他內容。

只是分開你的captcha和窗體,以便它會工作。

例如您captcha.php將是:

<?php 
session_start(); 
// build the base captcha image 

$img = imagecreatetruecolor(80, 30); 

$white = imagecolorallocate($img, 255, 255, 255); 
$black = imagecolorallocate($img, 0, 0, 0); 
$grey = imagecolorallocate($img, 150, 150, 150); 
$red = imagecolorallocate($img, 255, 0, 0); 
$pink = imagecolorallocate($img, 200, 0, 150); 

// build the base captcha image - END 

// building randomString 
function randomString($length) 
{ 
    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    srand((double)microtime() * 1000000); 
    $str = ""; 
    $i = 0; 

    while ($i <= $length) { 
     $num = rand() % 33; 
     $tmp = substr($chars, $num, 1); 
     $str = $str . $tmp; 
     $i++; 
    } 
    return $str; 
} 

for ($i = 1; $i <= rand(1, 5); $i++) { 
    $color = (rand(1, 2) == 1) ? $pink : $red; 
    imageline($img, rand(5, 70), rand(5, 20), rand(5, 70) + 5, rand(5, 20) + 5, $color); 
} 
// building randomString - END 

// fill image with a white rectangle 
imagefill($img, 0, 0, $white); 

$string = randomString(rand(7, 10)); 
$_SESSION['string'] = $string; 

imagettftext($img, 11, 0, 10, 20, $black, "Gotham-Bold.otf", $string); 
// fill image with a white rectangle - END 

// type of image 
header("Content-type: image/png"); 
imagepng($img); 


?> 
+0

好吧,我會嘗試...謝謝你... –

+0

謝謝..它正在工作... –

+0

@Igorbeginner,接受它,如果它......乾杯! – Rayon

相關問題