如何在wordpress插件中使用验证码

时间:2014-11-23 18:10:03

标签: php wordpress captcha

我创建了一个wordpress插件。此插件显示wordpress主题中的提交表单。我在我的wp插件中使用这个php验证码:

    function create_image() { 
    $md5_hash = md5(rand(0,999)); 
    $security_code = substr($md5_hash, 15, 5); 
    $_SESSION["security_code"] = $security_code;
    $width = 100; 
    $height = 35;  
    $image = ImageCreate($width, $height);  
    $white = ImageColorAllocate($image, 255, 255, 255); 
    $black = ImageColorAllocate($image, 2, 0, 0); 
    $blue = ImageColorAllocate($image, 87, 20, 186);
    $grey = ImageColorAllocate($image, 204, 204, 204); 
    ImageFill($image, 0, 0, $blue); 
    ImageString($image, 5, 30, 8, $security_code, $white); 
    ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
    header("Content-Type: image/jpeg"); 
    Imagepng($image); 
    ImageDestroy($image); 
}

并在img标签中使用验证码:

<img class="captcha" src="<?php create_image() ?>">

但是当我在我的主题中使用这个插件时,不要工作!

2 个答案:

答案 0 :(得分:0)

当你渲染它时,你的图像标签会说

      <img class="captcha" src="<<a whole bunch of binary stuff>>">

为什么呢?因为您的create_image()函数使用Imagepng()的单参数形式内联发出图像。

相反,您需要将图像发送到.png文件,然后在<img ... />标记中提及该文件的名称。

您可以将功能的最后几行改为这样工作。

   ...
   ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
   $file = "tmp-" . rand() . ".png";
   Imagepng($image, $file);
   return "/" . $file;
}

这将使您的函数创建一个带有随机名称的png文件,然后返回该文件的路径以在<img .../>标记中使用。

这需要一些调试。我编写它的方式,它假定您能够写入当前工作目录。您可能需要写入一些可供Web服务器访问的临时目录。

此外,这些图像文件将堆积起来。你需要做一些cron工作或其他东西来清理旧的。

答案 1 :(得分:-1)

试试这段代码..

<?php
    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }
    function generateCaptchaImage($text = 'good'){
        // Set the content-type
        header('Content-Type: image/png');
        $width  = 200;
        $height = 30;
        // Create the image
        $im = imagecreatetruecolor($width, $height);

        // Create some colors
        $white  = imagecolorallocate($im, 255, 255, 255);
        $grey   = imagecolorallocate($im, 128, 128, 128);
        $black  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 399, 29, $white);

        //ADD NOISE - DRAW background squares
        $square_count = 6;
        for($i = 0; $i < $square_count; $i++){
            $cx = rand(0,$width);
            $cy = (int)rand(0, $width/2);
            $h  = $cy + (int)rand(0, $height/5);
            $w  = $cx + (int)rand($width/3, $width);
            imagefilledrectangle($im, $cx, $cy, $w, $h, $white);
        }

        //ADD NOISE - DRAW ELLIPSES
        $ellipse_count = 5;
        for ($i = 0; $i < $ellipse_count; $i++) {
          $cx = (int)rand(-1*($width/2), $width + ($width/2));
          $cy = (int)rand(-1*($height/2), $height + ($height/2));
          $h  = (int)rand($height/2, 2*$height);
          $w  = (int)rand($width/2, 2*$width);
          imageellipse($im, $cx, $cy, $w, $h, $grey);
        }

        // Replace path by your own font path
        $font = 'ThisisKeSha.ttf';

        // Add some shadow to the text
        imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

        // Add the text
        imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

        // Using imagepng() results in clearer text compared with imagejpeg()
        imagepng($im);
        imagedestroy($im);
    }

    $randomString = generateRandomString();
    generateCaptchaImage($randomString);
?>

仍然没有得到这个Good Solution参与演示