登录页面的验证码怎么做啊?

时间:2022-04-20 17:11:16
各位大哥哥,我现在做了一个登陆页面,我想做一个有验证码的图片,输入验证码,具体怎么做?很着急,在线等啊

16 个解决方案

#1


需要后台(php、c#、java等)生成含随机字符的图片,javascript应该也可以,不过js是在客户端运行的东西,不安全……

#2


我就想要一简单的啊,怎么做啊,就像登录的时候,有个图片出来,有要填的数字,填了才能登录

#3


后台输出数据流 。前台那个"看不清"就是再次请求一下 ,这东西没有用js做的 。
不然失去意义 。

#4


那具体怎么做?可以告诉个详细的思路吗?具体怎么做?有代码吗???

#5


要写后台代码的?php、c#、java等你会哪样 我给你

#6


我会java,其他的不会。谢谢哥

#7


怎么给我呢???jiayeshi@163.com我的邮箱,发里面行吗?

#8


http://www.cnblogs.com/jqyp/archive/2010/09/01/1815006.html
原理就是生成随机数保存到Session中后,画出来用<img>标签显示,验证的时候将用户输入与Session中保存的验证码比对,这个是比较简单的……

#9


PHP用GD2就能轻易实现,原理就和楼上说的一样

#10


谢谢诶,我先研究一下

#11


我没学过PHP,等明天过了研究一下。

#12


决定不实现了,以后学习

#13


我有这个验证,你要不?

#14


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;  
    }  

}

【待续】

#15


前台就写主要的了啊
<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>

#16


用户登录验证码怎么做?
请看这里,有例子项目下载。

#1


需要后台(php、c#、java等)生成含随机字符的图片,javascript应该也可以,不过js是在客户端运行的东西,不安全……

#2


我就想要一简单的啊,怎么做啊,就像登录的时候,有个图片出来,有要填的数字,填了才能登录

#3


后台输出数据流 。前台那个"看不清"就是再次请求一下 ,这东西没有用js做的 。
不然失去意义 。

#4


那具体怎么做?可以告诉个详细的思路吗?具体怎么做?有代码吗???

#5


要写后台代码的?php、c#、java等你会哪样 我给你

#6


我会java,其他的不会。谢谢哥

#7


怎么给我呢???jiayeshi@163.com我的邮箱,发里面行吗?

#8


http://www.cnblogs.com/jqyp/archive/2010/09/01/1815006.html
原理就是生成随机数保存到Session中后,画出来用<img>标签显示,验证的时候将用户输入与Session中保存的验证码比对,这个是比较简单的……

#9


PHP用GD2就能轻易实现,原理就和楼上说的一样

#10


谢谢诶,我先研究一下

#11


我没学过PHP,等明天过了研究一下。

#12


决定不实现了,以后学习

#13


我有这个验证,你要不?

#14


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;  
    }  

}

【待续】

#15


前台就写主要的了啊
<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>

#16


用户登录验证码怎么做?
请看这里,有例子项目下载。