简体   繁体   English

PHP的验证码图像不显示

[英]php captcha image is not displaying

I am trying to make a simple php script for captcha but I don't know why it is not showing the captcha image. 我正在尝试为验证码创建一个简单的php脚本,但我不知道为什么它没有显示验证码图像。

here is the script : ` 这是脚本:

<?php

session_start();

header("Content-Type: image/png");
$im = imagecreate(110, 20)  or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
$possible_no="abcdefghjkmnpqrstuvwxyz23456789,@$";
$len=strlen($possible_no);
$random=mt_rand(0,$len);
$i=0;
while($i<=6){
$captcha .=substr($possible_no, mt_rand(0, strlen($possible_no)-1), 1); 
$i++;
}
$a="$captcha";
imagestring($im, 1, 5, 5,"$a", $text_color);
imagepng($im);
imagedestroy($im);
$_SESSION['captcha']=$a;
?>`

thanks in advance :) 提前致谢 :)

Make a new file (security_image.php) and put this in it: Maybe you might want it to display differently but you'll have to adapt what you want into it. 制作一个新文件(security_image.php)并将其放入其中:也许您可能希望它以不同的方式显示,但是您必须将所需的内容调整到其中。

session_start();

function generate_code(){

    $length = '6';
    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9', ',', '@', '$');

    $code = '';
    for ($i=0; $i < $length; $i++){

        $code .= $chars[rand(0, count($chars)-1)];
    }

    $_SESSION['captcha'] = $code;

    return $code;
}

function security_image(){

    $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();

    $font = 'content/fonts/comic.ttf';

    $width = '110';
    $height = '20';
    $font_size = $height * 0.75;
    $image = @imagecreate($width, $height) or die('GD not installed');

    $background_color = imagecolorallocate($image, 0, 0, 0);
    $text_color = imagecolorallocate($image, 233, 14, 91);


    $textbox = imagettfbbox($font_size, 0, $font, $code);
    $x = ($width - $textbox[4]) / 2;
    $y = ($height - $textbox[5]) / 2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);


    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);

}

security_image();

Then in your image html tag put this: 然后在您的图像html标记中输入以下内容:

<img src="security_image.php" alt="security code" />

Once you've done your process afterwards set: 完成流程后,设置:

unset($_SESSION['captcha']); // To reset the captcha

Hope it helps. 希望能帮助到你。 You should really make it so it regenerates all the time which you could do by changing the $code var in the security_image() function to do so. 您应该真正做到这一点,以便通过更改security_image()函数中的$code var来重新生成所有可能的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM