生成验证码我们会要用到php 图形处理函数,如imagecreate,imagepng,header之类的函数,下面我们一起来看个简单的实例。
实例代码如下:
- <?php
- session_start();
- $im = imagecreate(80,30);//创建图片
- $color = imagecolorallocate($im,rand(150,200),rand(150,200),rand(150,200));//设置图片背景
- $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";//产生随机字符串
- for($i=0;$i<5;$i++){
- $code .= $str[rand(0,(strlen($str)-1))];
- }
- $_SESSION['code'] = $code;
- for($a=0;$a<5;$a++){ //将字符串写入图片资源
- $x = $a*10 + 15;
- $y = rand(5,10); // www.111cn.net
- imagechar($im,5,$x,$y,$code{$a},imagecolorallocate($im,0,0,0));
- }
- header("Content-type:image/png");//输出图片资源
- imagepng($im);
- ?>
例2代码如下:
- <?php
- if(!isset($_SESSION)){ //判断session是否开启
- session_start(); //开启就session
- }
- $width=70; //布画宽度
- $height=25; //布画高度
- $length=4;//验证码长度
- $code=getcode($length); //获取随机字符串
- $_SESSION['verfyCode'] = $code;
- $img=imagecreate($width,$height);
- $bgcolor=imagecolorallocate($img,240,240,240);
- $rectangelcolor=imagecolorallocate($img,150,150,150);
- imagerectangle($img,1,1,$width-1,$height-1,$rectangelcolor);//画边框
- for($i=0;$i<$length;$i++){//循环写字
- $codecolor=imagecolorallocate($img,mt_rand(50,200),mt_rand(50,128),mt_rand(50,200));
- $angle=rand(-20,20);
- $charx=$i*15+8;
- $chary=($height+14)/2+rand(-1,1);
- imagettftext($img,15,$angle,$charx,$chary,$codecolor,'C:WINDOWSFontsSIMKAI.TTF',
- $code[$i]);
- }
- for($i=0;$i<20;$i++){//循环画线
- $linecolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
- $linex=mt_rand(1,$width-1);
- $liney=mt_rand(1,$height-1);
- imageline($img,$linex,$liney,$linex+mt_rand(0,4)-2,$liney+mt_rand(0,4)-2,$linecolor);
- }
- for($i=0;$i<100;$i++){//循环画点
- $pointcolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
- imagesetpixel($img,mt_rand(1,$width-1),mt_rand(1,$height-1),$pointcolor);
- }
- function getcode($length){//生成php随机数
- $pattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';//字符池
- for($i=0;$i<$length;$i++) {
- $key .= $pattern{mt_rand(0,35)};
- }
- return $key;
- }
- ob_clean();
- header('Content-type:image/png');
- imagepng($img);
- ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php入门级的PHP验证码程序
波比源码 » php入门级的PHP验证码程序
prix levitra en france 1 percent had received neoadjuvant chemotherapy, 0
how to get free viagra There is battled infertility and PCOS for years