登录页面的验证码怎么做啊? 各位大哥哥,我现在做了一个登陆页面,我想做一个有验证码的图片,输入验证码,具体怎么做?很着急,在线等啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 需要后台(php、c#、java等)生成含随机字符的图片,javascript应该也可以,不过js是在客户端运行的东西,不安全…… 要写后台代码的?php、c#、java等你会哪样 我给你 我会java,其他的不会。谢谢哥 怎么给我呢[email protected]我的邮箱,发里面行吗? http://www.cnblogs.com/jqyp/archive/2010/09/01/1815006.html原理就是生成随机数保存到Session中后,画出来用<img>标签显示,验证的时候将用户输入与Session中保存的验证码比对,这个是比较简单的…… PHP用GD2就能轻易实现,原理就和楼上说的一样 我没学过PHP,等明天过了研究一下。 java代码:import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.Random;import javax.imageio.ImageIO;import javax.imageio.stream.ImageOutputStream;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class CodeAction extends ActionSupport { private ByteArrayInputStream inputStream; public String execute() throws Exception{ System.out.println("code begin");// 在内存中创建图象 int width=55, height=20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 生成随机类 Random random = new Random(); // 设定背景色 g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height); // 设定字体 g.setFont(new Font("Times New Roman",Font.PLAIN,18)); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160,200)); for (int i=0;i<155;i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x,y,x+xl,y+yl); } // 取随机产生的认证码(4位数字) String sRand=""; for (int i=0;i<4;i++){ String rand=String.valueOf(random.nextInt(10)); sRand+=rand; // 将认证码显示到图象中 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawString(rand,13*i+6,16); } // 将认证码存入SESSION ActionContext.getContext().getSession().put("code",sRand); // 图象生效 g.dispose(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageOutputStream imageOut = ImageIO.createImageOutputStream(output); ImageIO.write(image, "JPEG", imageOut); imageOut.close(); ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); this.setInputStream(input); return SUCCESS; } /* * 给定范围获得随机颜色 */ private Color getRandColor(int fc,int bc){ Random random = new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); return new Color(r,g,b); } public void setInputStream(ByteArrayInputStream inputStream) { this.inputStream = inputStream; } public ByteArrayInputStream getInputStream() { return inputStream; } }【待续】 前台就写主要的了啊<script type="text/javascript"> function reloadCode(){ var timenow = new Date().getTime(); document.getElementById("codeImg").src="code.action?d="+timenow;}</script> <!-放到head标签中去--><td align="right" class="x-text"> 验证码: </td> <td> <input type="text" name="code" id="code" maxLength=4 size="7"/> </td> <td> <img alt="单击换一张" id="codeImg" src="code.action" onclick="reloadCode();"/> <!- 到java servelet配置文件或struts配置文件去配置去 这个不用我教了吧 呵呵--> </td> 遇到一个诡异的东西啊 请求EXTJS根据get返回的json数据动态生成tabspanel的demo 一个全局变量不起作用 下面这段javascript在IE下能work, FF下不行,怎么修改 为什么处理语句写在子函数中就不行了? 求通用合并行的js 紧急求助!js自动提交表单问题 怎么让IE5下打开的窗口只是最小化和关闭按钮? 一个WEB开发技术的专业网站<<WEB开发者>> 如何判断一个函数是否存在或者定义? js在ie下正常传值firefox没有值 js设置ip为可信站点IE6无效
原理就是生成随机数保存到Session中后,画出来用<img>标签显示,验证的时候将用户输入与Session中保存的验证码比对,这个是比较简单的……
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class CodeAction extends ActionSupport { private ByteArrayInputStream inputStream;
public String execute() throws Exception{
System.out.println("code begin");
// 在内存中创建图象
int width=55, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
// 设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}
// 将认证码存入SESSION
ActionContext.getContext().getSession().put("code",sRand);
// 图象生效
g.dispose();
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
imageOut.close();
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
this.setInputStream(input);
return SUCCESS;
}
/*
* 给定范围获得随机颜色
*/
private Color getRandColor(int fc,int bc){
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
} }【待续】
<script type="text/javascript">
function reloadCode(){
var timenow = new Date().getTime();
document.getElementById("codeImg").src="code.action?d="+timenow;
}
</script> <!-放到head标签中去-->
<td align="right" class="x-text">
验证码:
</td>
<td>
<input type="text" name="code" id="code" maxLength=4 size="7"/>
</td>
<td>
<img alt="单击换一张" id="codeImg" src="code.action" onclick="reloadCode();"/> <!- 到java servelet配置文件或struts配置文件去配置去 这个不用我教了吧 呵呵-->
</td>