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

php如何创建简单验证码

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

本文主要和大家分享php如何创建简单验证码的代码,希望能帮助到大家。

一、创建一个验证码输出文件

session_st<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong>art();//5.创建一个验证码$code_length = 4;$codes = '';for($i=0;$i<$code_length;$i++){    $codes .= dechex(mt_rand(0,15));}//7.生成sesstion$_SESSION['code'] = $codes;$width = 75;$height = 25;//1. 创建一张图片$img = imagecreatetruecolor($width,$height);//4. 创建图片静态内容//白色$white = imagecolorallocate($img, 255, 255, 255);//黑色$black = imagecolorallocate($img, 100, 100, 100);//红色$red = imagecolorallocate($img, 255, 0, 0);//图片背景imagefill($img, 0, 0, $white);//随机线条for($i=0;$i<4;$i++){    //创建随机颜色    $img_mt_color = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));    //画线    imageline($img, mt_rand(0,$width),mt_rand(0,$height), mt_rand(0,$width),mt_rand(0,$height), $img_mt_color);}//随机打雪花for($i=0;$i<100;$i++){    //创建随机浅淡色    $img_mt_color = imagecolorallocate($img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));    //画*    imagestring($img,1, mt_rand(0,$width),mt_rand(0,$height),'*',$img_mt_color);}//imagestring($img, 3, 10, 10, strlen($codes), $red);//6.绘制验证码for($i=0;$i<$code_length;$i++){    //创建验证码颜色(偏深色)    $img_mt_color = imagecolorallocate($img, mt_rand(0,100), mt_rand(0,150), mt_rand(0,200));    //生成验证码每一位字符    imagestring($img, mt_rand(3,5), $i*$width/4 + mt_rand(0,10), mt_rand(1,$height/2), $_SESSION['code'][$i], $img_mt_color);}//创建红色矩形边框imagerectangle($img, 0, 0, $width-1, $height-1, $red);//2. 让浏览器知道输出的是一张图片header('Content-type:image/png');//3. 输出imagepng($img);//4. 销毁imagedestroy($img);

二、在另一个页面调用这个验证码,并且实现点击刷新验证码:

<img src="php_code_img.php" id="myCodeImg" /><script type="text/javascript">(function(){    var myCodeImg = document.getElementById_x('myCodeImg');    myCodeImg.onclick = function()    {        this.src = './php_code_img.php?mt='+Math.ceil(Math.random()*1000);    }})();</script>

相关推荐:

利用PHP绘图函数实现简单验证码功能的方法_php实例

php实现简单验证码

基于PHP生成简单的验证码,PHP生成简单验证码_PHP教程

以上就是php如何创建简单验证码的详细内容,更多请关注搞代码gaodaima其它相关文章!


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

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

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

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

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