您的位置:首页 > 编程语言 > PHP开发

php创建最简单验证码

2017-01-20 17:03 477 查看

php创建最简单验证码

<?php
$char = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
$char_len = strlen($char);
$code='';

for($i = 1; $i <= 4; $i++){
$code_index = mt_rand(0, $char_len - 1);
$code = $code.''.$char[$code_index];
}
session_start();
$_SESSION['captcha_code'] = $code;

$img_path = 'captcha_bg'.mt_rand(1, 5).'.jpg';

$img = imagecreatefromjpeg($img_path);

$font = 5;
$size_width = imagesx($img);
$size_height = imagesy($img);
$font_width = imagefontwidth($font);
$font_height = imagefontheight($font);

$color = mt_rand(1,2) == 1 ? imagecolorallocate($img, 0, 0, 0) : imagecolorallocate($img, 0xff, 0xff, 0xff);

imagestring($img, $font, ($size_width-$font_width*4)/2, ($size_height-$font_height)/2, $code, $color);

header('Content-Type:image/jpeg;');

imageJPEG($img);
imageDestroy();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息