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

I create a wordpress plugin. This plugin show a submit form in wordpress theme. i use this php captcha code in my wp plugin:

    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); 
}

and use captcha in img tag:

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

But when i use this plugin in my theme, don't worked!

Your image tag will, when you render it, say

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

Why? because your create_image() function emits the image inline using the single-parameter form of Imagepng().

Instead, you need to emit the image to a .png file, then mention that file's name in your <img ... /> tag.

You could change the last few lines of your function to work like this.

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

This will make your function create a png file with a random name and then return the path to that file to be used in your <img .../> tag.

This will take some debugging. The way I have written it, it assumes that you are able to write to the current working directory. You probably need to write to some temp directory that's accessible to your web server instead.

Also, these image files will pile up. You'll need to cook up some cron job or something to clean out the old ones.

try this code..

<?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);
?>

Still u r not getting then refer this Good Solution with Demo