验证码的类及使用

<?php

session_start();

ini_set(‘display_errors‘, ‘Off‘);

class CCheckCodeFile

{

//验证码位数 www.2cto.com

var $mCheckCodeNum  = 4;

//产生的验证码

var $mCheckCode   = ‘‘;

//验证码的图片

var $mCheckImage  = ‘‘;

//干扰像素

var $mDisturbColor  = ‘‘;

//验证码的图片宽度

var $mCheckImageWidth = ‘80‘;

//验证码的图片宽度

var $mCheckImageHeight  = ‘20‘;

//输出头

function OutFileHeader()

{

header ("Content-type: image/png");

}

//产生验证码

function CreateCheckCode()

{

//$this->mCheckCode = strtoupper(substr(md5(rand()),0,$this->mCheckCodeNum));

$this->mCheckCode = strtoupper(substr(rand(0,9999999999),0,$this->mCheckCodeNum));

session_cache_expire(60);

session_start();

$_SESSION["code_str"] = $this->mCheckCode;

return $this->mCheckCode;

}

//产生验证码图片

function CreateImage()

{

$this->mCheckImage = @imagecreate ($this->mCheckImageWidth,$this->mCheckImageHeight);

imagecolorallocate ($this->mCheckImage, 255, 255, 255);

return $this->mCheckImage;

}

//设置图片的干扰像素

function SetDisturbColor()

{

for ($i=0;$i<=128;$i++)

{

$this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));

imagesetpixel($this->mCheckImage,rand(1,100),rand(1,100),$this->mDisturbColor);

}

}

//设置验证码图片的大小\宽\高

function SetCheckImageWH($width,$height)

{

if($width==‘‘||$height==‘‘)return false;

$this->mCheckImageWidth  = $width;

$this->mCheckImageHeight = $height;

return true;

}

//在验证码图片上逐个画上验证码

function WriteCheckCodeToImage()

{

for ($i=0;$i<=$this->mCheckCodeNum;$i++)

{

$bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));

$x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;

$y = rand(0,$this->mCheckImageHeight-15);

imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);

}

}

//输出验证码图片

function OutCheckImage()

{

$this ->OutFileHeader();

$this ->CreateCheckCode();

$this ->CreateImage();

$this ->SetDisturbColor();

$this ->WriteCheckCodeToImage();

imagepng($this->mCheckImage);

imagedestroy($this->mCheckImage);

}

}

$c_check_code_image = new CCheckCodeFile();

//$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸

$c_check_code_image->OutCheckImage();

?>

使用方法:

<tr>

<td align="center" valign="middle">验证码</td>

<td align="center" valign="middle"><input name="number" type="text" style="width:80px;">

<img src="code.php" id="yzm" onclick="this.src=‘code.php?abc=‘+Math.random()" alt="图片看不清?点击重新得到验证码" style="cursor:hand;" />

</td>

</tr>

验证是否正确

session_start();

if($_POST["number"] == $_SESSION["code_str"]){

//      echo("验证码正确");

}else{

echo "

<script>

var s=10;

function ok()

{

s--;

document.getElementById(‘tips‘).innerHTML=s;

if(s==0)

{

clearTimeout(t);

history.go(-1);

}

}

var t=setInterval(‘ok()‘,1000);

</script>

" . "请输入的验证码,请重新输入<span id=‘tips‘></span>秒之后,将为您跳转到<a href=\"javascript:history.go(-1)\">上一页</a>";

}

时间: 2024-11-09 10:21:01

验证码的类及使用的相关文章

自己封装————验证码工具类————

下面分享的是我自己封装的验证码工具类,在平时的项目中会比较经常用到的工具类,目前封装的这个工具类简易版的,如果有需要的伙伴可以拿去用,当然我建议用之前在配置文件里增加一些选项信息 //验证码宽度 private $width; //验证码高度 private $height; //验证的个数 private $length; //干扰点个数 private $dots; //干扰点的类型 private $type; //干扰线个数 private $lines; //文字 private $f

一个生成网页验证码的类 (mycome-validate)

一个小练习 可以通过 BufferedImage next() 返回一个内存图片 也可以通过String void next(OutputStream out) 写到一个输出流中,并返回验证码的值 jar包下载:http://files.cnblogs.com/mycome/mycome-validate.zip package validate; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; i

生成验证码的类

<%@ WebHandler Language="C#" Class="ValidateCode" %> using System; using System.Web; using System.Drawing; using System.Web.SessionState; public class ValidateCode : IHttpHandler,IRequiresSessionState { public void ProcessRequest

图片验证码工具类

图片验证码工具类 文章 https://blog.csdn.net/lzxlfly/article/details/93381526 需求 session中放入登录验证码,一定时间后定时清除. 每次使用过验证码后清除,需要重新生成验证码. 工具类 package com.yuantiao.smartcardms.util; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; i

Java 验证码工具类

验证码代码工具代码如下: import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;import java.

生成随机验证码工具类

生成随机验证码 package com.web; //验证码生成处理类 import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.geom.Line2D; import java.awt.ima

Laravel 下生成验证码的类

<?php namespace App\Tool\Validate; //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子 private $code;//验证码 private $codelen = 4;//验证码长度 private $width = 130;//宽度 private $height = 50;//高度 private

php实现的IMEI限制的短信验证码发送类

<?php class Api_Sms{ const EXPIRE_SEC = 1800; // 过期时间间隔 const RESEND_SEC = 60; // 重发时间间隔 const ONE_DAY_FREQ = 5; // 每日向同一个手机号发短信的次数 const ONE_DAY_IMEI_COUNT = 3; // 每日向同一个手机号发送短信的IMEI个数 public $error = array(); /** * 向指定手机号发送验证码 * @param $mobile * @p

php验证码生成类

<?php    class Upload {        //确定属性        private $_max_size;        private $_ext_list= array('.jpg','.png','.gif');        private $_allow_mine_list= array('image/pgn','image/gif','image/jpeg','image/pjpeg','image/x-png');        private $_uploa