一个简单的PHP验证码实现代码
实现代码:
<?php
//将验证码保存到session里,供全局使用
session_start();
$nums="";
for($i=0;$i<4;$i++){
//产生随机数并转换成十六进制
$nums.=dechex(mt_rand(0,15));
}
//将验证码写入session
$_SESSION["code"]=$nums;
//设置验证码长和宽
$_width=60;
$_height=20;
//创建一张图片
$_img=imagecreatetruecolor($_width,$_height);
//创建一个白色
$_white=imagecolorallocate($_img,220,250,250);
//填充背景
imagefill($_img,0,0,$_white);
//随机画6条线条
for($i=0;$i<6;$i++){
$_rnd_color=imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_width),mt_rand(0,$_width),mt_rand(0,$_width),$_rnd_color);
}
//随机画出雪花
for($i=0;$i<60;$i++){
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),"*",imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
}
//输出验证码
for($i=0;$i<strlen($_SESSION["code"]);$i++){
imagestring($_img,mt_rand(6,10),$i*$_width/4+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION["code"][$i],imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}
//输出和销毁
header("Content-Type:image/png");
imagepng($_img);
imagedestroy($_img);
?>
相关文章
- 微信小程序+php 授权登陆,完整代码
- 笛卡尔积 php,PHP笛卡尔积实现算法示例
- PHP 文件上传漏洞代码
- PHP助力MySQL实现程序开发梦想(php支持mysql)
- 联合MySQL与PHP:双剑合璧(mysql与php)
- PHP开启Redis:实现快速访问(php开启redis)
- 实现缓存PHP开启Redis超越缓存技术(php开启redis)
- 使用PHP实现Redis快速构建应用(redis实例php)
- 使用PHP驱动程序实现与SQL Server数据库的连接(php连接sqlserver)
- PHP代码更高效:使用MongoDB类(phpmongodb类)
- PHP连接MSSQL数据库:探索新的接口(php连接mssql接口)
- PHP连接Microsoft SQL Server数据库技巧(php 连 mssql)
- PHP编写的MSSQL查询:实现可能性无限(php写mssql查询)
- 仅用PHP操作MSSQL,实现快速下载!(php mssql下载)
- PHP图片上传至MySQL:实现快速可靠的数据存储(php图片上传mysql)
- 在字符串指定位置插入一段字符串的php代码
- 并发下常见的加锁及锁的PHP具体实现代码
- php中显示数组与对象的实现代码
- php中使用parse_url()对网址进行解析的实现代码(parse_url详解)
- PHP异步调用socket实现代码
- PHP读取数据库并按照中文名称进行排序实现代码
- 解析PHP计算页面执行时间的实现代码
- 邮箱正则表达式实现代码(针对php)
- PHP读取大文件的X行到Y行内容的实现代码
- php修改、增加xml结点属性的实现代码
- php分页函数示例代码分享
- 使用PHP导出Redis数据到另一个Redis中的代码
- PHP处理SQL脚本文件导入到MySQL的代码实例
- php实现的DateDiff和DateAdd时间函数代码分享