2014-04-17 85 views
0

这里是我的/tools/showCaptcha.php为什么注册Captcha没有显示?

<?php 
session_start(); 
header('Content-Type: image/png'); 
/** 
* This file generates a captcha string, writes it into the $_SESSION['captcha'] 
* and renders a fresh captcha graphic file to the browser. 
* 
* In the views you can use this by saying: 
* <img src="tools/showCaptcha.php" /> 
* 
* Check if the typed captcha is correct by saying: 
* if ($_POST["captcha"] == $_SESSION['captcha']) { ... } else { ... } 

*/ 

// target captcha string length 

$iCaptchaLength = 4; 

// following letters are excluded from captcha: I, O, Q, S, 0, 1, 5 

$str_choice = 'ABCDEFGHJKLMNPRTUVWXYZ2346789'; 
$str_captcha = ''; 

// create target captcha with letters coming from $str_choice 

for ($i = 0; $i < $iCaptchaLength; $i++) 
    { 
    do 
     { 
     $ipos = rand(0, strlen($str_choice) - 1); 

     // checks that each letter is used only once 

     } 

    while (stripos($str_captcha, $str_choice[$ipos]) !== false); 
    $str_captcha.= $str_choice[$ipos]; 
    } 

// write the captcha into a SESSION variable 

$_SESSION['captcha'] = $str_captcha; 

// begin to create the image with PHP's GD tools 

$im = imagecreatetruecolor(150, 70); 
$bg = imagecolorallocate($im, 255, 255, 255); 
imagefill($im, 0, 0, $bg); 

// create background with 1000 short lines 

/*for ($i = 0; $i < 1000; $i++) 
    { 
    $lines = imagecolorallocate($im, rand(200, 220) , rand(200, 220) , rand(200, 220)); 
    $start_x = rand(0, 150); 
    $start_y = rand(0, 70); 
    $end_x = $start_x + rand(0, 5); 
    $end_y = $start_y + rand(0, 5); 
    imageline($im, $start_x, $start_y, $end_x, $end_y, $lines); 
    } 
*/ 
// create letters. for more info on how this works, please 
// @see php.net/manual/en/function.imagefttext.php 
// TODO: put the font path into the config 

for ($i = 0; $i < $iCaptchaLength; $i++) 
    { 
    $text_color = imagecolorallocate($im, rand(0, 100) , rand(10, 100) , rand(0, 100)); 

    // font-path relative to this file 

    imagefttext($im, 35, rand(-10, 10) , 20 + ($i * 30) + rand(-5, +5) , 35 + rand(10, 30) , $text_color, '/fonts/times_new_yorker.ttf', $str_captcha[$i]); 
    } 

// send http-header to prevent image caching (so we always see a fresh captcha image) 

header('Pragma: no-cache'); 
header('Cache-Control: no-store, no-cache, proxy-revalidate'); 

// send image to browser and destroy image from php "cache" 

imagepng($im); 
imagedestroy($im); 
?> 

我做错什么了吗?我尝试了所有我能想到的改变,但恢复到第一个版本。它在我将其全部添加到我的模板文件中之前工作。但我找不到任何会改变的理由。

所有显示的都是小小的“PNG Not Found”绘画。

+0

基于GD的验证码在某些托管服务器中不起作用。 您是否尝试过reCAPTCHA? https://developers.google.com/recaptcha/docs/php –

+1

我想我应该更新这个。我实际上转向了那个昨天(27日)。感谢您看看! – dewdn2

回答

0

尝试使用reCAPTCHA在某些托管服务器中,基于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

0

非常感谢tharindu_DG和任何其他人看看我的问题。 tharindu_DG引起了我的注意,基于GD的验证码不适用于某些服务器。在我有机会编辑这篇文章之前,我转向了reCAPTCHA,它的工作完美无瑕。

再次感谢tharindu_DG!