创建一个新文件 (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();
然后在您的图像 html 标记中输入:
<img src="security_image.php" alt="security code" />
一旦你完成了你的过程,然后设置:
unset($_SESSION['captcha']); // To reset the captcha
希望能帮助到你。你真的应该让它重新生成,你可以通过更改$code
security_image() 函数中的 var 来做到这一点。