php编写验证码
今天学习到了php登录时的验证码,验证码在我们平时的网站建设中是非常重要的,对于放置一些灌水机、脚本攻击是一个很好地策略。
下面是我写的代码:
<?php
session_start();
//产生一个随机的字符串验证码
$checkcode="";
for ($i=0;$i<4;$i++){
$checkcode.=dechex(rand(0,15)); //string dechex ( int $number ) 返回一字符串,包含有给定 number 参数的十六进制表示
}
//将随机验证码保存到session中
$_SESSION['myCheckCode']=$checkcode;
//创建图片,并把上面产生的随机验证码画上去
$img=imagecreatetruecolor(100, 20);
//背景默认是黑色,可以自己设定背景颜色
$bgcolor=imagecolorallocate($img, 0, 0, 0);
//imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
imagefill($img, 0, 0, $bgcolor);
//创建新的颜色 imagecolorallocate — 为一幅图像分配颜色
//imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。
//red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。
//imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色
$white=imagecolorallocate($img, 255, 255, 255);
$blue=imagecolorallocate($img, 0, 0, 255);
$red=imagecolorallocate($img, 255, 0, 0);
$green=imagecolorallocate($img, 255, 0, 0);
//画出干扰线段
for($i=0;$i<10;$i++){
//bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
//imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
imageline($img, rand(0, 100), rand(0, 20), rand(0, 100), rand(0, 20), imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255)));
}
//画出噪点
//for(){}
//把上面产生的四个随机值,字符串画上去
//bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
//imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。
//如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagestring($img, rand(2, 5), rand(2, 60), rand(2, 5), $checkcode, $white);
header("content-type:image/png");
//imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。
imagepng($img);
?>
php编写验证码的更多相关文章
- js编写验证码
这是一个简单的js编写的验证码,自己已经亲自验证,没有问题了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- asp.net 编写验证码
首先准备一个类来实现对验证码的绘制功能. createcode.cs using System; using System.Collections.Generic; using System.Linq ...
- java servlet编写验证码
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buffere ...
- Java编写验证码
Java后台代码(CheckCodeServlet.java) package web; import java.awt.Color; import java.awt.Font; import jav ...
- java验证码(采用struts2实现)转
第一步:编写验证码的Action package com; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; ...
- C#验证码使用
1.C#创建验证码 1.1 创建获取验证码页面(ValidateCode.aspx) <html xmlns="http://www.w3.org/1999/xhtml"&g ...
- Spring Security 实战干货:从零手写一个验证码登录
1. 前言 前面关于Spring Security写了两篇文章,一篇是介绍UsernamePasswordAuthenticationFilter,另一篇是介绍 AuthenticationManag ...
- ASP.NET MVC 5 02 - ASP.NET MVC 1-5 各版本特点
参考书籍:<ASP.NET MVC 4 高级编程>.<ASP.NET MVC 5 高级编程>.<C#高级编程(第8版)>.<使用ASP.NET MVC开发企业 ...
- HttpHander与httpModel配置与应用
ASP.NET对请求处理的过程: 当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给 ASPNET_ISAPI.dll, ...
随机推荐
- 第六章_PHP数组
1.PHP支持两种数组:索引数组(indexed array)和关联数组(associative array),前者使用数字作为键,后者使用字符串作为键. 2.遍历索引数组 2.1 for循环语句 2 ...
- Js/Ajax中发送HttpPost请求调用WebService
1) WebService中的方法 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(Confo ...
- Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:
七月 17, 2014 4:56:01 下午 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service( ...
- mysql基本数据类型(mysql学习笔记三)
Mysql数据类型 小数: 浮点:小数位可以变化 Float单精度默认精度6位左右 Double 双精度默认精度16位左右 支持,控制数值范围 Type(M,D) M表示所有数值位数(不包括小数点和符 ...
- nginx php 安装
.选定源码目录选定目录 /data/klj/ cd /data/klj/ 2.安装PCRE库cd /data/klj/wget ftp://ftp.csx.cam.ac.uk/pub/software ...
- 仿酒仙网的一款jQuery侧栏弹出导航栏特效
仿酒仙网的一款jQuery侧栏弹出导航栏特效 一款常用于商城左侧商品导航的jquery菜单导航特效. 非常不错的一款商品分类特效.大家可以拿去研究研究 . 注意:该特效还支持挨千刀的IE6啊,之强大. ...
- PHP入门基础(一)——标记风格、注释、表单获取、字符串类型、变量解析
PHP标记风格: //XML风格//推荐的标记风格,可以在XML文档中使用 <?php echo '<p>XML Style</p>'; ?> //简短风格——需启 ...
- 通过Messenger与后台连接(单向操作,activity向service发送数据)
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- Android的ADT内容助手快捷方式设置
请注明出处:http://www.cnblogs.com/killerlegend/p/3550019.html Written By KillerLegend 先将Word Completion的 ...
- MVC MVVM Knockout viewmodel 提交 完整过程,包含序列化 JSON 和 字典模型绑定
//JSON 通用操作------------------------------------------------------------------------------using Syste ...