驗證碼的使用(Java和PHP版)

驗證碼的作用大部分都是爲減少服務器壓力而設計的,這玩意也挺古老的,談不上什麼新奇。無論是java還是php生成驗證碼的原理都一樣。

1.php驗證碼

<?php
//checkNum.php
session_start();
function random($len)
{
$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();//配置亂數種子
$strs="";
for($i=0;$i <$len;$i++){
$strs.=$srcstr[mt_rand(0,35)];
}

return strtoupper($strs);
}
$str=random(4); //隨機生成的字符串" title="字符串" >字符串
$width = 50; //驗證碼圖片的寬度
$height = 20; //驗證碼圖片的高度

@header("Content-Type:image/png");
$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊點顏色
$pix=imagecolorallocate($im,187,230,247);
//字體色
$font=imagecolorallocate($im,41,163,238);
//繪模糊作用的點
mt_srand();
for($i=0;$i <1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 5,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
$_SESSION["code"] = $str;

?>

基本上都是先調用一個繪圖的方法,把圖像放到輸出流,之後顯示在頁面上


2.java版驗證碼

java這塊就麻煩點,生成驗證碼的程序一般寫在servlet裏,這種是最簡單的方式。稍複雜的可以用一些第三方的驗證碼jar包

(1)數字版驗證碼

package org.lxh;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.awt.*;
import java.awt.image.*;
import java.util.*;

import javax.imageio.*;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageCode2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 在內存中創建圖象
		int iWidth = 60, iHeight = 18;
		BufferedImage image = new BufferedImage(iWidth, iHeight,
				BufferedImage.TYPE_INT_RGB);
		// 獲取圖形上下文
		Graphics g = image.getGraphics();
		// 設定背景色
		g.setColor(Color.white);
		g.fillRect(0, 0, iWidth, iHeight);
		// 畫邊框
		g.setColor(Color.black);
		g.drawRect(0, 0, iWidth - 1, iHeight - 1);
		// 取隨機產生的認證碼(4位數字)
		Random random = new Random();
		String rand = String.valueOf(random.nextInt(10000));
		// rand=rand.substring(0,rand.indexOf("."));
		switch (rand.length()) {
		case 1:
			rand = "000" + rand;
			break;
		case 2:
			rand = "00" + rand;
			break;
		case 3:
			rand = "0" + rand;
			break;
		default:
			rand = rand;
			break;
		}
		// 將認證碼存入SESSION
		request.getSession().setAttribute("Rand", rand);
		// 將認證碼顯示到圖象中
		g.setColor(Color.black);
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		g.drawString(rand, 10, 15);
		// 隨機產生88個干擾點,使圖象中的認證碼不易被其它程序探測到
		// Random random = new Random();
		for (int iIndex = 0; iIndex < 88; iIndex++) {
			int x = random.nextInt(iWidth);
			int y = random.nextInt(iHeight);
			g.drawLine(x, y, x, y);
		}
		// 圖象生效
		g.dispose();

		ServletOutputStream outstream = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outstream);

		encoder.encode(image);
		outstream.flush();
	}

}

之後在頁面上請求這個servlet,驗證碼就出來了,頁面代碼如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>驗證碼的使用</title>

</head>
<script language="javascript" type="text/javascript">
  function changeImage(img){
   
   img.src="ImageCode?t="+new Date().getTime();
  }
 </script>
<body>
    輸入驗證碼:<input type="text"/><img src="ImageCode" id="img" onclick="changeImage(this)" style="cursor: pointer;"/>
</body>
</html>


頁面效果如下:


(2)稍微複雜一點的驗證碼

package org.lxh;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageCode extends HttpServlet {
	// 定義可選擇的字符
	public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
			'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
			'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
	static Random random = new Random();

	public String getRandomString() {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < 6; i++) { // 生成六個字符
			buffer.append(CHARS[random.nextInt(CHARS.length)]);
		}
		return buffer.toString();
	}

	public static Color getRandomColor() { // 得到隨機顏色
		return new Color(random.nextInt(255), random.nextInt(255),
				random.nextInt(255));
	}

	public static Color getReverseColor(Color c) { // 得到顏色的反色
		return new Color(255 - c.getRed(), 255 - c.getGreen(),
				255 - c.getBlue());
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("image/jpeg"); // 設置輸出類型
		String randomString = getRandomString();
		System.out.println(randomString);
		// 將getSession()設置爲true,當會話不存在是返回null
		request.getSession(true).setAttribute("ImageCode", randomString);

		// 設置圖片的寬、高
		int width = 100;
		int height = 30;

		Color bcolor = getRandomColor(); // 前景色
		Color fcolor = getReverseColor(getRandomColor()); // 設置背景色

		// 創建一個彩色圖片
		BufferedImage bimage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_BGR);
		// 創建繪圖對象
		Graphics2D g = bimage.createGraphics();
		// 字體樣式爲宋體,加粗,20磅
		g.setFont(new Font("宋體", Font.BOLD, 20));
		// 先畫出背景色
		g.setColor(bcolor);
		g.fillRect(0, 0, width, height);
		// 再畫出前景色
		g.setColor(fcolor);
		// 繪製隨機字符
		g.drawString(randomString, 20, 22);
		// 畫出干擾點
		for (int i = 0, n = random.nextInt(100); i < n; i++) {
			g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
		}

		ServletOutputStream outstream = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outstream);

		encoder.encode(bimage);
		outstream.flush();

	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
}

這種方式的驗證碼是下面這個樣子:


後面的工作就簡單了,如果要看驗證碼對不對,直接從session裏取出和輸入框裏的比對一下就OK了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章