验证码实例如下看不懂的先看gd库的那一篇,session不懂的看from提交的数据都哪里了那一篇,欢迎关注

index.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form name="form" method="post" action="">
验证码(大写):<br>
<img src="hcon.php" width="70" height="18" border="0" align="bottom">
<input type="text" name="yanzhengma">
<br>
<input type="Submit" name="Submit" value="登录" >
</form>
</body>
</html>
<?php
session_start();
if(isset($_POST["Submit"]) && $_POST["Submit"]!=""){
$yanzhengma=$_POST["yanzhengma"]; if($yanzhengma=="")
{ echo "<script> alert('验证码为空');window.location.href='index.php';</script>";}
else if($yanzhengma!=$_SESSION["jiancha"]){
echo "<script> alert('输错了');window.location.href='index.php';</script>";
}
else{
echo "<script> alert('登录成功');window.location.href='index.php';</script>";
} } ?>

yanzhengma.php

<?php
class yanzhengma2{
protected $width;
protected $height;
protected $im;
protected $len;
protected $code; public function __construct($width=100,$height=30,$len=5){
$this->width=$width;
$this->height=$height;
$this->len=$len;
} public function render(){
$im= imagecreatetruecolor($this->width,$this->height);
$color=imagecolorallocate($im, 200,200,200);
imageFill($this->im=$im,0,0,$color);//画布背景填充颜色,默认黑色
$this->text();
$this->line();
$this->pix();
$this->show();
return $this->code; } //显示渲染
protected function show(){
header('Content-type:image/png');
imagepng($this->im);
} //绘制验证码
protected function text(){
$font=$font="c:/windows/fonts/simhei.ttf";
$text='abcdefghigklmnopqrstuvwxyz0123456789';
for($i=0;$i<$this->len;$i++){
$x=$this->width/$this->len;
$angle=mt_rand(-20,20);
$this->code.=$textn=strtoupper($text[mt_rand(0,strlen($text)-1)]);
$box=imagettfbbox(20,$angle,$font,$textn); imagettftext(
$this->im,
20,$angle,
$x*$i+10,
$this->height/2-($box[7]-$box[1])/2,
$this->textcolor(),
$font,
$textn
);
}
}
//绘制干扰点
protected function pix(){
for($i=0;$i<300;$i++){
imagesetpixel($this->im,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->color());
} }
//绘制干扰线
protected function line(){ for($i=0;$i<6;$i++){
imagesetthickness($this->im,mt_rand(1,2));
imageline($this->im,
mt_rand(0,$this->width),
mt_rand(0,$this->height),
mt_rand(0,$this->width),
mt_rand(0,$this->height),
$this->color()
);
}
}
//生成随机颜色
protected function color(){
return imagecolorallocate($this->im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
} //生成随机文字颜色(深)
protected function textcolor(){
return imagecolorallocate($this->im,mt_rand(0,50),mt_rand(0,50),mt_rand(0,50));
}
} ?>

hcon.php

<?php
session_start();
include 'yanzhengma.php';
$yanzhengma=new yanzhengma2(200,40);
$code=$yanzhengma->render();
$_SESSION["jiancha"]=$code;
?>

php验证码实例的更多相关文章

  1. JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)

    1.转发和重定向 HttpServletResponse response 转发: RequestDispatcher dispatcher = request.getRequestDispatche ...

  2. C#生成验证码实例

    常用生成验证码实例封装: /// <summary> /// 生成内存位图 /// </summary> /// <param name="Code" ...

  3. Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一)

    原文:Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一) 拓展压缩包的使用方式详细介绍 1:将拓展包解压:ThinkPHP3.1.2_Extend.zip   --> 将其下的 \ ...

  4. 5种PHP生成图片验证码实例

    5种PHP生成图片验证码实例,包括数字验证码.数字+字母验证码.中文验证码.仿google验证码和算术验证码,PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session ...

  5. DAY5 python内置函数+验证码实例

    内置函数 用验证码作为实例 字符串和字节的转换 字符串到字节 字节到字符串

  6. php_curl模拟登录有验证码实例

    <?php/** * @author 追逐__something * @version $id */define('SCRIPT_ROOT',dirname(__FILE__).'/');$ac ...

  7. Django学习-25-图片验证码实例

    处理流程 用户请求网页 --> 后台发送登录界面的静态页面 --> 后台在内存中生成验证码 --> 验证码保存在用户对应的Session中 --> 返回验证码图片到前端 用户登 ...

  8. 使用C#的aforge类库识别验证码实例

    一: 验证码处理 1.  一般处理原则 这种验证码为什么说跟没有一样,第一点:字体规范工整,第二点:不旋转扭曲粘连,第三点:字体颜色单一,下面看处理步骤. 这里要注意的是,aforge只接受像素格式为 ...

  9. spring mvc 使用kaptcha配置生成验证码实例

    SpringMVC整合kaptcha(验证码功能) 一.依赖 <dependency> <groupId>com.github.penggle</groupId> ...

随机推荐

  1. 发布一个npm package

    1. 创建一个package.json文件 发布到npm registry的包必须包含一个packge.json文件. 1. 必需name字段 要求: 1. 只能是一个单词,但是可以包含-或_ 2. ...

  2. 【luogu4474王者之剑】--网络流

    题目描述 这是在阿尔托利亚·潘德拉贡成为英灵前的事情,她正要去拔出石中剑成为亚瑟王,在这之前她要去收集一些宝石. 宝石排列在一个n*m的网格中,每个网格中有一块价值为v(i,j)的宝石,阿尔托利亚·潘 ...

  3. Luogu5072 [Ynoi2015]盼君勿忘 【莫队】

    题目描述:对于一个长度为\(n\)的序列,\(m\)次询问\(l,r,p\),计算\([l,r]\)的所有子序列的不同数之和\(\mathrm{mod} \ p\). 数据范围:\(n,m,a_i\l ...

  4. grafana+mysql忘记admin密码解决方法

    通过mysql修改admin的密码. 我之前注册了一个用户,所以我直接复制lvusyy这个用户的密码即可. update user inner join (select password,salt,r ...

  5. Intellij IDEA常用配置记录

    换个IDE试试. 一个地址 http://intellij.mandroid.cn/ http://idea.imsxm.com/ http://idea.iteblog.com/key.php TO ...

  6. python smbus IOError: [Errno 2] No such file or directory

    1.打开配置文件 sudo nano /boot/config.txt 打开以下选项 "dtparam=i2c_arm=on" ctrl + o 保存 ctrl + x 退出 2. ...

  7. Java基础系列 - equals和==的区别

    package com.test7; public class test7 { public static void main(String[] args) { /** * 1.equals()比较的 ...

  8. 2.线程--线程安全(synchronized)

    同步锁:synchronized,(缺点:不能手动开锁 解锁)   1.同步代码块 public void run(){ synchronized(this){/使用this关键字表示同一把锁,非静态 ...

  9. Hbase 错误记录分析(1) region超时问题

    错误现象: 默认等待时间是60秒,超过这个时间就报超时问题了.因此需调整超时时间,默认为60秒,在配置文件 hbase-site.xml中: 调整成10分钟 <property>    & ...

  10. 【转】Python基础-字符串

    原文地址http://blog.chinaunix.net/uid-21169302-id-446256.html Python-String-Function 字符串中字符大小写的变换: * S.l ...