码迷,mamicode.com
首页 > Web开发 > 详细

php验证码制作

时间:2016-03-13 15:56:17      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

目标: 使用php生成验证码

成品:

    技术分享

逻辑代码: authcode.php

<?php
header("Content-type:image/png");
session_start();
//$str用于存放验证码
$str="";
//$charset中剔除了0,o,1,l等易混淆字符
$cs="abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345678923456789";
$img=imagecreate($width=100,$height=26);
//图片背景色使用浅色调
$img_bg=imagecolorallocate($img,rand(186,255), rand(186,255), rand(186,255));
for($i=0;$i<4;++$i){
    //文字颜色使用深色调
    $txt_color=imagecolorallocate($img,rand(0,86),rand(0,86),rand(0,86));
    //从$charset中随机出来一个字符
    $tmp=substr($cs,rand(0,strlen($cs)-1),1);
    //进行偏移和显示
    imagestring($img,5,$i*24+rand(6,15),rand(2,12),$tmp,$txt_color);
    $str.=$tmp;
}
//将验证码值放入session中以备后用(验证用户输入的验证码)
$_SESSION["authcode"]=$str;
//添加背景线条
for($j=0;$j<5;++$j){
    $line_color=imagecolorallocate($img,rand(150,210), rand(150,210), rand(150,210));
    imageline($img, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color);
}
//添加噪点
for($i=0;$i<250;++$i){
    $img_noisy=imagecolorallocate($img,rand(150,210), rand(150,210), rand(150,210));
    imagesetpixel($img,rand(0,$width),rand(0,$height),$img_noisy);
}
imagepng($img);
imagedestroy($img);


前端使用: register.html

<img src="authcode.php"/>

 

php验证码制作

标签:

原文地址:http://www.cnblogs.com/iovec/p/5272146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!