PHP生成图片验证码、点击切换实例_php实例

WBOY
Release: 2016-05-17 08:40:46
Original
880 people have browsed it

这里来看下效果:

现在让我们来看下 PHP 代码

复制代码 代码如下:

 
session_start();
function random($len) {
    $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
    mt_srand();
    $strs = "";
    for ($i = 0; $i         $strs .= $srcstr[mt_rand(0, 30)];
    }
    return $strs;
}
 
//随机生成的字符串
$str = random(4);
 
//验证码图片的宽度
$width  = 50;     
 
//验证码图片的高度
$height = 25;    
 
//声明需要创建的图层的图片格式
@ header("Content-Type:image/png");
 
//创建一个图层
$im = imagecreate($width, $height);
 
//背景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
 
//模糊点颜色
$pix  = imagecolorallocate($im, 187, 230, 247);
 
//字体色
$font = imagecolorallocate($im, 41, 163, 238);
 
//绘模糊作用的点
mt_srand();
for ($i = 0; $i     imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}
 
//输出字符
imagestring($im, 5, 7, 5, $str, $font);
 
//输出矩形
imagerectangle($im, 0, 0, $width -1, $height -1, $font);
 
//输出图片
imagepng($im);
 
imagedestroy($im);
 
$str = md5($str);
 
//选择 cookie
//SetCookie("verification", $str, time() + 7200, "/");
 
//选择 Session
$_SESSION["verification"] = $str;
?>

接下来只要在页面中调用就可以了:

复制代码 代码如下:

PHP生成图片验证码、点击切换实例_php实例

如果想实现 "看不清?换一张" 效果,添加如下 JS 到页面中

复制代码 代码如下:

function changing(){
    document.getElementById('checkpic').src="/images/checkcode.php?"+Math.random();
}
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template