dsdtszi0520538 2014-11-23 18:10 采纳率: 100%
浏览 63
已采纳

如何在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!

  • 写回答

2条回答 默认 最新

  • doufan2541 2014-11-23 18:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题