2014-02-06 40 views
1

如何使用PHP和GD庫創建與此相同的驗證碼?在PHP/GD庫中創建自定義驗證碼

來源:http://www.aphrodite-agency.com/assets/captcha/captcha.php

圈中是有效的,只有字母或數字...

的index.php:

<?php 
session_start(); 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
    <title>Demo captcha</title> 
    </head> 
    <body> 
    <form method="POST" action=""> 
     <h3>Quel est ce chiffre ?</h3> 
     <img src="captcha.php" /><br /> 
     <input type="text" name="captcha" style="width:70px"/><br /> 
     <input type="submit" /> 
    </form> 
<?php 
if($_POST['captcha']){ 
    if($_POST['captcha']==$_SESSION['captcha']) echo '<h3 style="color:green">Bingo !</h3>'; 
    else echo '<h3 style="color:red">Oups !</h3>'; 
} 
?> 
    </body> 
</html> 

captcha.php:

<?php 
    session_start(); 
    $_SESSION['captcha'] = rand(1000,9999); 

$img = imagecreatetruecolor(70, 30); 

$fill_color=imagecolorallocate($img,255,255,255); 
imagefilledrectangle($img, 0, 0, 70, 30, $fill_color); 
$text_color=imagecolorallocate($img,10,10,10); 
$font = './28DaysLater.ttf'; 
imagettftext($img, 23, 0, 5,30, $text_color, $font, $_SESSION['captcha']); 

header("Content-type: image/jpeg"); 
imagejpeg($img); 
imagedestroy($img); 
?> 
+1

第1步,停止重新發明車輪並使用類似recaptcha的東西。 –

+0

這是否給出了任何錯誤或您對我們的期望? – Bram

回答

0

嘗試使用reCA PTCHA在某些託管服務器中,基於GD的驗證碼不起作用。我經歷過它。 它可以與GD庫一起使用。 它從外部服務加載驗證碼圖像並使用公鑰加密進行驗證。 所以內部生成驗證碼圖像。 非常容易使用。

E.G:

前端:

<html> 
    <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers --> 
     <!-- your HTML content --> 

     <form method="post" action="verify.php"> 
     <?php 
      require_once('recaptchalib.php'); 
      $publickey = "your_public_key"; // you got this from the signup page 
      echo recaptcha_get_html($publickey); 
     ?> 
     <input type="submit" /> 
     </form> 

     <!-- more of your HTML content --> 
    </body> 
    </html> 

後端驗證:

<?php 
    require_once('recaptchalib.php'); 
    $privatekey = "your_private_key"; 
    $resp = recaptcha_check_answer ($privatekey, 
           $_SERVER["REMOTE_ADDR"], 
           $_POST["recaptcha_challenge_field"], 
           $_POST["recaptcha_response_field"]); 

    if (!$resp->is_valid) { 
    // What happens when the CAPTCHA was entered incorrectly 
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . 
     "(reCAPTCHA said: " . $resp->error . ")"); 
    } else { 
    // Your code here to handle a successful verification 
    } 
    ?> 

欲瞭解更多信息請訪問:https://developers.google.com/recaptcha/docs/php

相關問題