驗(yàn)證碼必須由后端來(lái)做 前端只是顯示了一張圖片而已并不知道圖片上驗(yàn)證碼的內(nèi)容 就php來(lái)說(shuō) 原理是利用php的隨機(jī)函數(shù)申城一串驗(yàn)證碼存儲(chǔ)到session 然后利用gd2吧驗(yàn)證碼做成一張圖片顯示在前端 用戶在輸入驗(yàn)證碼的時(shí)候是吧涌入輸入的值與session的值作對(duì)比來(lái)確定用戶是否輸入正確

創(chuàng)新互聯(lián)是一家專業(yè)提供灌陽(yáng)企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為灌陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
要用手機(jī)驗(yàn)證碼功能,你需要向通訊服務(wù)商(移動(dòng)、聯(lián)通、電信)申請(qǐng)一個(gè)號(hào)碼,用于給客戶發(fā)送驗(yàn)證短信
下面是關(guān)鍵代碼,如果剩下的你都搞不懂,我就無(wú)語(yǔ)了
JS
script type="text/javascript" language="javascript"
function reloadcodeOne(){//刷新驗(yàn)證碼函數(shù)
var verify = document.getElementById('checkCodeImg');
verify.setAttribute('src', 'validateCode?dt=' + Math.random());
}
script type="text/javascript"
html
p
label驗(yàn)證碼:/label
input class="code" value="請(qǐng)輸入驗(yàn)證碼" title="請(qǐng)輸入驗(yàn)證碼" name="rendCode" id="rendCode" onfocus="if (value =='請(qǐng)輸入驗(yàn)證碼'){value =''}" onblur="if (value ==''){value='請(qǐng)輸入驗(yàn)證碼'}" 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
* 校驗(yàn)碼控制器
* /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;
// 驗(yàn)證碼圖片的寬度。
private int width = 10;
// 驗(yàn)證碼圖片的高度。
private int height = 5;
// 驗(yàn)證碼字符個(gè)數(shù)
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";
// 字符個(gè)數(shù)
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用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證。
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編寫出驗(yàn)證碼效果,你可以參考
!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('您通過(guò)驗(yàn)證了!');
}?else?{
alert('您輸入的驗(yàn)證碼不正確!');
}
;
}
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
希望可以幫助到你
在html中的文本框中加入驗(yàn)證碼,可以通過(guò)以下代碼實(shí)現(xiàn):
驗(yàn)證碼通過(guò)GD生成PNG圖片,并把$randval隨機(jī)數(shù)字賦給
$_SESSION['login_check_num'],在通過(guò)用戶輸入的$_POST進(jìn)行比較,來(lái)判斷是否正確。達(dá)到需要實(shí)現(xiàn)的功能,需要修改php.ini文件,使php支持GD庫(kù)。
?php
//調(diào)用此頁(yè)面,如果下面的式子成立,則生成驗(yàn)證碼圖片
if($_GET["action"]=="verifycode")
{
rand_create();
}
//驗(yàn)證碼圖片生成
function rand_create()
{
//通知瀏覽器將要輸出PNG圖片
Header("Content-type: image/PNG");
//準(zhǔn)備好隨機(jī)數(shù)發(fā)生器種子
srand((double)microtime()*1000000);
//準(zhǔn)備圖片的相關(guān)參數(shù)
$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0); //RGB黑色標(biāo)識(shí)符 $white =
ImageColorAllocate($im, 255,255,255); //RGB白色標(biāo)識(shí)符 $gray = ImageColorAllocate($im,
200,200,200); //RGB灰色標(biāo)識(shí)符 //開始作圖
imagefill($im,0,0,$gray);
while(($randval=rand()%100000)10000);{
$_SESSION["login_check_num"] = $randval;
//將四位整數(shù)驗(yàn)證碼繪入圖片
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); }
//輸出驗(yàn)證圖片
ImagePNG($im);
本文題目:php實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接,php實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接的方法
標(biāo)題鏈接:http://chinadenli.net/article3/dsgjjos.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站設(shè)計(jì)、全網(wǎng)營(yíng)銷推廣、做網(wǎng)站、品牌網(wǎng)站制作、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)