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,一经查实,立即删除!

相关文章

写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…

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…

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

可靠消息最终一致性设计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…

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

游戏用户体验指标游戏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…

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

名词解释:对等知识互联网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…

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

饥饿的盛世读后感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 …

figma 安装插件_我制作Figma插件的经验

figma 安装插件Since Figma released the Figma Community (Beta), I’ve been working on Figma plugins in my free time while I study the code. With the help of an engineer friend of mine, I’ve developed four small plugins so far. As I continue to update these…

术语解释_术语

术语解释Colour has a great impact in the world around us and this is no different in User Interfaces (UI). However, it’s not always given the importance it deserves. Sometimes colour is understood as a purely aesthetic element that is completely relative …

mac photoshop_我讨厌Photoshop…

mac photoshopIt probably sounds odd to hear a visual designer say they hate Photoshop. It’s sort of like hearing a writer say they hate Word. It’s sort of a given that Photoshop is the medium within which visual designers work their magic. It’s also one…

做事用人 用人做事_做事:构建我的第一个Web应用程序的经验教训

做事用人 用人做事On the 5th of June, 2020, after almost two weeks of (re)learning javascript, fixing bugs, creating new ones and of course, lots of testing, I launched Writty on ProductHunt. An open-source text editor to help anyone who is into writing to …

[转]C#委托的异步调用

本文将主要通过“同步调用”、“异步调用”、“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊。 首先&#xff0c;通过代码定义一个委托和下面三个示例将要调用的方法&#xff1a; /*添加的命名空间using System.Threading;using System.Runtime.…

vista下载_Vista和视图在游戏设计中的功能

vista下载Views in video games are observation points used to highlight a lot of objects into one frame or shot using a special camera move. Vistas are special types of views that show distant objects, mainly far off landscapes.电子游戏中的视图是观察点&…

keynote使用手册_如何使用Keynote和智能手机为AR创建原型

keynote使用手册Designing for AR is perhaps one of the most interesting applications of UX. As this incredible technology is being put to use for unique applications, UX Designers are tasked with creating user interfaces for an augmented experience, that do…