• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一个简单的PHP验证码实现代码_php实例

php 搞代码 4年前 (2022-01-26) 22次浏览 已收录 0个评论

实现代码:

<?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 = imagecreat本文来源gao@daima#com搞(%代@#码@网&搞gaodaima代码etruecolor($_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);
?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一个简单的PHP验证码实现代码_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址