重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
下面是關鍵代碼,如果剩下的你都搞不懂,我就無語了
創新互聯公司-成都網站建設公司,專注成都網站建設、網站設計、網站營銷推廣,申請域名,網頁空間,網站托管、服務器托管有關企業網站制作方案、改版、費用等問題,請聯系創新互聯公司。
JS
script type="text/javascript" language="javascript"
function reloadcodeOne(){//刷新驗證碼函數
var verify = document.getElementById('checkCodeImg');
verify.setAttribute('src', 'validateCode?dt=' + Math.random());
}
script type="text/javascript"
html
p
label驗證碼:/label
input class="code" value="請輸入驗證碼" title="請輸入驗證碼" name="rendCode" id="rendCode" onfocus="if (value =='請輸入驗證碼'){value =''}" onblur="if (value ==''){value='請輸入驗證碼'}" type="text" size="6" /
spanimg id="checkCodeImg" src="validateCodeServlet" onclick="javascript:reloadcodeOne();" alt="" width="75" height="24" //span
/p
java代碼
package com.zhihui.action.common;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.zhihui.action.base.BaseAction;
/**
* p
* 校驗碼控制器
* /p
*
* @author liurong
* @version ValidateCodeServlet.java,v 0.1 2008-11-20 上午09:22:31 Administrator
* Exp
*/
public class ValidateCodeAction extends BaseAction {
private static final long serialVersionUID = 1L;
// 驗證碼圖片的寬度。
private int width = 10;
// 驗證碼圖片的高度。
private int height = 5;
// 驗證碼字符個數
private int codeCount = 5;
private int x = 0;
// 字體高度
private int fontHeight;
private int codeY;
/*char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };*/
char[] codeSequence = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
HttpServletRequest req=ServletActionContext.getRequest();
HttpServletResponse resp=ServletActionContext.getResponse();
public String execute()
throws ServletException, java.io.IOException {
// 寬度
String strWidth = "70";
// 高度
String strHeight = "22";
// 字符個數
String strCodeCount = "5";
width = Integer.parseInt(strWidth);
height = Integer.parseInt(strHeight);
codeCount = Integer.parseInt(strCodeCount);
x = width / (codeCount);
fontHeight = height - 4;
codeY = height - 4;
// 定義圖像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
Random random = new Random();
// 將圖像填充為白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
g.setColor(Color.BLACK);
for (int i = 0; i 15; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(8);
int yl = random.nextInt(8);
g.drawLine(x, y, x + xl, y + yl);
}
// randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
for (int i = 0; i codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
red = 0;//random.nextInt(255);
green = 0;//random.nextInt(255);
blue = 0;//random.nextInt(255);
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i ) * x, codeY);
randomCode.append(strRand);
}
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
return null;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getCodeCount() {
return codeCount;
}
public void setCodeCount(int codeCount) {
this.codeCount = codeCount;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getFontHeight() {
return fontHeight;
}
public void setFontHeight(int fontHeight) {
this.fontHeight = fontHeight;
}
public int getCodeY() {
return codeY;
}
public void setCodeY(int codeY) {
this.codeY = codeY;
}
public char[] getCodeSequence() {
return codeSequence;
}
public void setCodeSequence(char[] codeSequence) {
this.codeSequence = codeSequence;
}
public HttpServletRequest getReq() {
return req;
}
public void setReq(HttpServletRequest req) {
this.req = req;
}
public HttpServletResponse getResp() {
return resp;
}
public void setResp(HttpServletResponse resp) {
this.resp = resp;
}
}
你好,可以使用canvas編寫出驗證碼效果,你可以參考
!DOCTYPE?HTML
html?lang="en"
meta?charset="utf-8"
script?type="text/javascript"?src="js/jquery-1.8.3.min.js"/script
style
@charset?"utf-8";
/*?CSS?Document?*/
body?{
background:?url(images/img10.jpg)?no-repeat?fixed;
}
body,form,ul,ol,li,p,h1,h2,h3,h4,h5,h6,dl,dt,dd,table,fieldset,hr,div?{
margin:?0;
padding:?0;
}
body,input,select,textarea?{
color:?#000;
font:?12px/1.8?"微軟雅黑",?Arial,?Helvetica,?sans-serif;
}
img?{
border:?0;
vertical-align:?middle;
}
table?{
width:?100%;
border:?0;
border-collapse:?collapse;
border-spacing:?0;
}
ul,ol,li?{
list-style-type:?none;
}
a?{
color:?#000;
outline:?none;
text-decoration:?none;
}
a:hover?{
text-decoration:?underline;
}
.contain?{
width:?500px;
margin:?0?auto;
padding-top:?200px;
}
/style
body
div?class="contain"
canvas?id="myCanvas"?height="300px"?width="500px"your?browser?does?not?support?the?canvas?tag?/canvas
br?/?input?type="text"
button?onClick="pass()"提交/button
/div
/body
script?type="text/javascript"
var?canvas?=?$("#myCanvas").get(0);
var?_canvas?=?$("#myCanvas").get(0).getContext("2d");
var?return_str?=?"";
var?_ifstart?=?false;
var?_B_x?=?0;
var?_B_y?=?0;
function?can_click()?{
};
function?pass()?{
var?_val?=?$(":text:eq(0)").val();
if?(_val?==?return_str)?{
alert('您通過驗證了!');
}?else?{
alert('您輸入的驗證碼不正確!');
}
;
}
function?start()?{
try?{
function?drawscreen()?{
_canvas.fillStyle?=?"#ffffaa";
_canvas.fillRect(0,?0,?500,?300);
_canvas.strokeStyle?=?"#000";
_canvas.strokeRect(5,?5,?490,?290);
}
;
function?write_text(_str)?{
_canvas.fillStyle?=?"#000000";
_canvas.font?=?"20px?_sans";
_canvas.textBaseline?=?"top";
_canvas.fillText(_str,?195,?80);
}
;
function?getabc()?{
var?_str?=?"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9";
var?_str_array?=?_str.split(",");
return_str?=?"";
for?(i?=?0;?i??4;?i++)?{
var?_rnd?=?Math.floor(Math.random()?*?_str_array.length);
return_str?+=?_str_array[_rnd];
}
;
}
;
drawscreen();
getabc();
write_text(return_str);
}?catch?(e)?{
alert(e);
}
};
$(document).ready(function(e)?{
start();
});
/script
/html
希望可以幫助到你
驗證碼必須由后端來做 前端只是顯示了一張圖片而已并不知道圖片上驗證碼的內容 就php來說 原理是利用php的隨機函數申城一串驗證碼存儲到session 然后利用gd2吧驗證碼做成一張圖片顯示在前端 用戶在輸入驗證碼的時候是吧涌入輸入的值與session的值作對比來確定用戶是否輸入正確
驗證碼一直錯誤那就是:后臺生成的和前臺顯示的不是同一個驗證碼,你可以把后臺生成的打印出來測試一下。
1:驗證碼生成代碼:
//輸入驗證碼
function verifyimg(){
//驗證碼做配置
$config = array(
'seKey' = 'ThinkPHP.CN', // 驗證碼加密密鑰
'codeSet' = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY', // 驗證碼字符集合
'expire' = 1800, // 驗證碼過期時間(s)
'useZh' = false,
'useImgBg' = false, // 使用背景圖片
'fontSize' = 14, // 驗證碼字體大小(px)
'useCurve' = true, // 是否畫混淆曲線
'useNoise' = true, // 是否添加雜點
'imageH' = 45, // 驗證碼圖片高度
'imageW' = 100, // 驗證碼圖片寬度
'length' = 4, // 驗證碼位數
'fontttf' = '4.ttf', // 驗證碼字體,不設置隨機獲取
'bg' = array(243, 251, 254), // 背景顏色
'reset' = true, // 驗證成功后是否重置
);
$verify=new Verify($config); //實例化Verify類。 空間類元素引入。使用次數多的情況下。
//verify= new \Think\Verify 完全限定名稱方式。 使用次數少的情況下。
$verify-entry(); //數據驗證碼。
}
2.下面的是一段驗證碼顯示代碼:
ul
li class="user_main_text"驗證碼: /li
li class="user_main_input"
input class="TxtValidateCodeCssClass" id="captcha" name="captcha" type="text"
驗證碼輸出
img src="{$smarty.const.__CONTROLLER__}/verifyimg" onclick="this.src='{$smarty.const.__CONTROLLER__}/verifyimg/'+Math.random()" alt="" /
/li
/ul
希望對你有幫助。
在html中的文本框中加入驗證碼,可以通過以下代碼實現:
驗證碼通過GD生成PNG圖片,并把$randval隨機數字賦給
$_SESSION['login_check_num'],在通過用戶輸入的$_POST進行比較,來判斷是否正確。達到需要實現的功能,需要修改php.ini文件,使php支持GD庫。
?php
//調用此頁面,如果下面的式子成立,則生成驗證碼圖片
if($_GET["action"]=="verifycode")
{
rand_create();
}
//驗證碼圖片生成
function rand_create()
{
//通知瀏覽器將要輸出PNG圖片
Header("Content-type: image/PNG");
//準備好隨機數發生器種子
srand((double)microtime()*1000000);
//準備圖片的相關參數
$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0); //RGB黑色標識符 $white =
ImageColorAllocate($im, 255,255,255); //RGB白色標識符 $gray = ImageColorAllocate($im,
200,200,200); //RGB灰色標識符 //開始作圖
imagefill($im,0,0,$gray);
while(($randval=rand()%100000)10000);{
$_SESSION["login_check_num"] = $randval;
//將四位整數驗證碼繪入圖片
imagestring($im, 5, 10, 3, $randval, $black);
}
//加入干擾象素
for($i=0;$i200;$i++){
$randcolor =
ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); }
//輸出驗證圖片
ImagePNG($im);
要用手機驗證碼功能,你需要向通訊服務商(移動、聯通、電信)申請一個號碼,用于給客戶發送驗證短信