PHP图形图像的典型应用 --常用图像的应用(验证码)

php生成动态的验证码,是php防止恶意登陆或者注册等常规手段-废话不多说,直接看例子。(只是一个简单的应用,如果要安全或者更复杂的,请期待我以后的文章)

PHP生成验证码核心文件 (checks.php):

<?php/*成生随机的验证码。此实例只是一个简单的例子,如果不想被人识别,还可以加噪点等干扰技术*/session_start(); //启动sessionheader("Content-type:image/x-png"); //设置创建图像的格式$image_width = 70;  //设置图像的宽度$image_height = 18;  //设置图像的高度srand(microtime()*100000);  //设置随机数的种子。 --这里关于srand和microtime函数请自行查阅php手册for($i=0; $i<4; $i++)    //循环输出一个4位数的随机数
    {$new_number.=dechex(rand(0,15)); //将十进制随机数转为十六进制
    }/*将获取的随机数验证码写入到Session变量中  --这里注意php的变量作用范围,和别的高级语言可能有些不同,所以要理解,自己去查手册*/$_SESSION['check_checks'] = $new_number;$num_image = imagecreate($image_width,$image_height);  //创建一个画布imagecolorallocate($num_image,255,255,255);  //设置画布的颜色for($i=0; $i<strlen($_SESSION['check_checks']); $i++)  //循环读取session变量中的验证码
    {$font = mt_rand(3,5); //设置随机的字体大小$x = mt_rand(1,8)+$image_width*$i/4;  //设置随机字符所在的位置的X坐标$y = mt_rand(1,$image_height/4);  //设置随机字符所在的位置的Y坐标//设置字符的颜色$color = imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//水平画出一行字符串 --输出字符,此函数自行查阅php手册。要GD2库支持imagestring($num_image,$font,$x,$y,$_SESSION['check_checks'][$i],$color);}imagepng($num_image); //生成png格式的图像imagedestroy($num_image);  //结束图像,释放资源
?>
View Code

PHP页面验证文件,判断是否和生成的验证码相同 (login.php):

<?phpheader("Content-type:text/html;charset=utf-8");session_start();  //初始化sessionif($_POST['checks'] != "")  //判断是否请求过来的为空
    {//如果不为空进行一个正则的替换,替换掉所有的空格 preg_replace()函数中用''而不是""来表示$checks = preg_replace('/[\s| ]/','',$_POST['checks']);echo "<script type='text/javascript'>prompt('这是您输入的验证码:','$checks');</script>";if($checks == ""){echo "<script type='text/javascript'>alert('验证码不能为空');window.location.href='index.php';</script>";    }//如果用户输入验证码的值与随机生成的验证码的值相等,则弹出登录成功提示if($checks == $_SESSION['check_checks']){echo "<script type='text/javascript'>alert('用户登录成功');window.location.href='index.php';</script>";    }else{echo "<script type='text/javascript'>alert('您输入的验证码不正确!');window.location.href='index.php';</script>";    }}else{echo "<script type='text/javascript'>alert('您没有输入验证码!');window.location.href='index.php';</script>";    }
?>
View Code

页面呈现登陆文件 (index.html或者index.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">//封装javascript的trim去掉字符串空格功能。function trim(strToTrim){//用正则来返回替换后的字符。--功能,去掉所有的空格!包括换行符等。 关于正则知识,请自行查阅资料return strToTrim.replace(/(\s+)/g,"");}function yz(){//先用js检测是否已经输入验证码,减少服务器压力,当然服务器那边也是要验证的if(trim(form1.checks.value) == ""){alert("对不起!你没有输入验证码!");return false; //返回flase不提交
        }return true;}
</script>
</head><body><div id="main"><form action="login.php" method="post" name="form1"><label>验证码:</label><input type="text" name="checks" /><img src="checks.php" title="验证码" alt="验证码显示错误" /><br /><input type="submit" value="提交验证" onclick="return yz();" /></form></div>
</body>
</html>
View Code

 

转载于:https://www.cnblogs.com/clouds008/archive/2013/06/11/3131970.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274210.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

接口练习

前台代码&#xff1a; <form id"form1" runat"server"> <div> 见面时间:<asp:TextBox ID"MeetTime" runat"server"></asp:TextBox><br /> 见面地点:<asp:TextBox ID"MeetAddr…

写saas创业的书_我在SaaS创业公司担任UX设计师的第一个月中学到的三件事

写saas创业的书I recently transitioned from being a copywriter at an ad agency to a UX Designer at a SaaS startup. To add more multidisciplinary skills into the mix, I graduated with a Bachelor in Accountancy.我最近从一名广告代理商的撰稿人过渡到了SaaS初创公…

ui项目答辩中学到了什么_我在UI设计9年中学到的12件事

ui项目答辩中学到了什么重点 (Top highlight)I know these can seem a bit clich but I will try to explain everything from my own experience.我知道这些内容似乎有些陈词滥调&#xff0c;但我会尝试根据自己的经验来解释所有内容。 第一名 (No.1 Never assume) The first…

linux下命令行的使用:使用sed命令操作文件

用该命令sed删除文件test.txt中包含某个字符串abc的行: sed /adc/d test.txt >result.txt 在文件test.txt中删除从开头到含有某个字符串abc的行 sed 1,/abc/d test.txt >result.txt 获取文件test.txt中包含字符串abc的行 cat test.txt |grep "abc" > resul…

ux的重要性_UX中清晰的重要性

ux的重要性重点 (Top highlight)Times, since the very first occurrences of web design in the 90’s, have changed a lot design-wise. The particular technology and its applications got more stable. Human-computer interaction (HCI) was deeply researched, design…

工欲善其事,必先利其器

vs2010中一些常用的快捷键、组合键&#xff1a; 1、快速格式化 CtrlED 2、注释选中部分 CtrlEC 3、停止调试 ShiftF5 4、取消注释选中部分 CtrlEU 5、显示解决方案资源管理器 CtrlWS 6、快速折叠 CtrlMO 7、封装一个字段 CtrlRE 8、查看属性 CtrlWP 9…

可靠消息最终一致性设计_如何最终启动您的设计产品组合

可靠消息最终一致性设计It’s not a secret that most designers procrastinate on their portfolios whether it is to update them or to create them in the first place.大多数设计师在更新产品组合时还是拖延产品组合并不是秘密。 首先创建它们 。 Hopefully, by the e…

台式机共享笔记本的无线网络(只需要一根网线)

以windowsXP系统为例 一、笔记本的设置方法&#xff1a; 1.先将笔记本的无线连接共享给有线网卡 将鼠标放到桌面的 网上邻居 上&#xff0c;按鼠标右键&#xff0c;选属性&#xff0c;再将鼠标放到无线连接上&#xff0c;又是按鼠标右键&#xff0c;选属性&#xff0c;在弹出的…

游戏用户体验指标_电子游戏如何超越游戏化的用户体验

游戏用户体验指标游戏UX (GAMES UX) During a time when the time spent on video games has reached record breaking heights, due to excessive time indoors, gamification has more of a place now than ever before.d uring的时候花在视频游戏的时间已经达到了 破纪录的高…

JAVA编程心得-JAVA实现CRC-CCITT(XMODEM)算法

CRC即循环冗余校验码&#xff08;Cyclic Redundancy Check&#xff09;&#xff1a;是数据通信领域中最常用的一种差错校验码&#xff0c;其特征是信息字段和校验字段的长度可以任意选定。 1 byte checksum CRC-16 CRC-16 (Modbus) CRC-16 (Sick) …

什么字体字母和数字大小一样_字母和字体如何适应我们的屏幕

什么字体字母和数字大小一样Writing went through many iterations before it became what is today. Times New Roman wasn’t the default script for ancient Egyptians, in fact, paper didn’t even exist when the first words were written.写作经历了许多迭代&#xff…

jenkins 通过批处理自动构建 非标准项目

之前介绍了java和vs2010的项目构建&#xff0c;这些都是比较常见的&#xff0c;所以都用专门的工具。但但难免会遇到一些不常见的项目&#xff0c;下面介绍通过批处理进行构建&#xff0c;并用jenkins调用.我们这里使用plc语言&#xff0c;没有标准环境&#xff0c;只有使用bat…

效果图底图 线框图_5分钟的线框图教程

效果图底图 线框图为什么使用线框&#xff1f; (Why wireframe?) Simply put, wireframes provide a structure and layout for content and assets.简而言之&#xff0c;线框提供了内容和资产的结构和布局。 You can wireframe just about any kind of presentation, from p…

多线程 - 你知道线程栈吗

问题 1. local 变量的压栈和出栈过程 void func1(){ int a 0; int b 0; } 系统中有一个栈顶指针&#xff0c;每次分配和回收local 变量时&#xff0c;其实就是移动栈指针。 2. static local变量的分配风险 void func2(){ static int a 0; } 这个变量a可能会被分…

怎么让qt发声_第3部分:添加网络字体-让我们的单词发声

怎么让qt发声This is a big week for the project. While it was an important step last week to establish some basic responsiveness, we couldn’t really nail down the typography until we added the typeface. Too many aspects of the feel, proportions, and overal…

mysql语句中把string类型字段转datetime类型

mysql语句中把string类型字段转datetime类型在mysql里面利用str_to_date&#xff08;&#xff09;把字符串转换为日期此处以表h_hotelcontext的Start_time和End_time字段为例&#xff0c;查询当前时间在此范围之内的数据。 www.2cto.com select * from h_hotelcontext where …

名词解释:对等知识互联网_网站设计理论:比较和对等

名词解释:对等知识互联网Equivalence and contrast, connection and distinction, categorization and non-categorization are all ways to distinguish the same or different elements. Based on the information they carry, we hope that the equivalent elements can hav…

hadoop深入研究:(五)——Archives

转载请注明来源地址&#xff1a;http://blog.csdn.net/lastsweetop/article/details/9123155 简介 我们在hadoop深入研究:(一)——hdfs介绍里已讲过&#xff0c;hdfs并不擅长存储小文件&#xff0c;因为每个文件最少一个block&#xff0c;每个block的元数据都会在namenode节点占…

人民币小写金额转大写金额

#region 人民币小写金额转大写金额/// <summary>/// 小写金额转大写金额/// </summary>/// <param name"Money">接收需要转换的小写金额</param>/// <returns>返回大写金额</returns>public static string ConvertMoney(Decimal…

饥饿的盛世读后感_满足任何设计师饥饿感的原型制作工具

饥饿的盛世读后感Tell me if this story sounds familiar to you. You just wrapped up a design in Sketch -a design that took you hours, and now you want to bring it to life. Sketch’s built-in prototyping tool doesn’t allow you to create all the interactions …