導航:首頁 > 萬維百科 > 網頁設計怎麼加驗證碼用c

網頁設計怎麼加驗證碼用c

發布時間:2021-02-18 09:04:19

1、如何在設計網頁的時候添加進驗證嗎?!

不用
<TR vAlign=top>
<TD>
<p align="center"> 驗證碼:
<%dim num1,rndnum
Randomize
Do While Len(rndnum)<4
num1=CStr(Chr((57-48)*rnd+48))
rndnum=rndnum&num1
loop
session("verifycode")=rndnum
%>
<input class="input" type="text" name="verifycode" size="15" font face="宋體" style="font-size: 9pt">
<b><span style="background-color: #FFFFFF"><font color=#000000><%=session("verifycode")%></font></span></b> </TD>
</TR>
應該就好了
如果不行,可以去參考別人的源代碼!

2、網頁設計中如何編寫驗證碼?

伺服器端生成隨機數,存在session裡面,然後把帶有數字的圖片返回到客戶端;
客戶端提交數據時判斷輸入的驗證碼是否與伺服器端的一致;
這樣可以達到防止惡意攻擊的目的.

3、關於網頁上的驗證碼,用VC怎麼做識別

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GenerateCheckCode : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
string chkCode = string.Empty;
//顏色列表,用於驗證碼、噪線、噪點
Color[] color =...{ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字體列表,用於驗證碼
string[] font =...{ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//驗證碼的字元集,去掉了一些容易混淆的字元
char[] character =...{ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成驗證碼字元串
for (int i = 0; i < 4; i++)
...{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(100, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//畫噪線
for (int i = 0; i < 10; i++)
...{
int x1 = rnd.Next(100);
int y1 = rnd.Next(40);
int x2 = rnd.Next(100);
int y2 = rnd.Next(40);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//畫驗證碼字元串
for (int i = 0; i < chkCode.Length; i++)
...{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 18);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8);
}
//畫噪點
for (int i = 0; i < 100; i++)
...{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//清除該頁輸出緩存,設置該頁無緩存
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
//將驗證碼圖片寫入內存流,並將其以 "image/Png" 格式輸出
MemoryStream ms = new MemoryStream();
try
...{
bmp.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
}
finally
...{
//顯式釋放資源
bmp.Dispose();
g.Dispose();
}
}
}

使用方法如下:
新建名為 GenerateCheckCode.aspx 的文件,將上述代碼拷貝到代碼文件 GenerateCheckCode.aspx.cs
在需要驗證碼的地方放置語句 <asp:Image ID="img1" runat="server" ImageUrl="~/GenerateCheckCode.aspx" /> 即可。

4、如何用c語言實現驗證碼的校驗?

什麼校驗方法?
CRC檢驗如下:

#include<stdio.h>

int binaryToDec(char *str)
{
unsigned n=0;
while(*str!='\0')
{
if(*str<'0'||*str>'9')return -1;
n=n*2+(*str-'0');
str++;
}
return n;
}

void printBinary(int n)
{
if(n>1)printBinary(n/2);
printf("%d",n%2);
}

void main()
{
unsigned n,m,CRC=0x1A8000,fD=0x100000;
char CRC16[32];

while(1)
{
printf("輸入16位校驗碼:");
gets(CRC16);
n=binaryToDec(CRC16);
if(n>65535)
printf("輸入值過長,請重新輸入\n");
else break;
}

n<<=5;//n左移5位
m=n;//m等於
while(fD>0x20)
{
while( !(m&fD) && !(CRC&1) )//保證被除數第一位為1
{
CRC>>=1;//除數右移一位
fD>>=1;//被除數首位的標志位右移一位
}
m=m^CRC;//被除數與除數相異或
}
n+=m;//模二餘數相加

printf("輸出21位校驗碼:");
printBinary(n);
printf("\n");

getchar();
}

5、在visual studio2010中的Web項目設計中怎麼添加驗證碼這個功能

做一個公共類,或者公共頁面,專門用來生成 隨機碼,每次提取一組,存入Session ,生成驗證碼圖片。調用時將輸入的值與存入的值對比即可。點擊刷新驗證碼就是重新獲取生成一次,重新存入Session。。
private void CreateCheckCode()
{
string checkCode = GetRandWord(5);
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
CreateCheckImage(checkCode);
}
/// <summary>
/// 生成驗證碼圖片
/// </summary>
/// <param name="checkCode"></param>
private void CreateCheckImage(string checkCode)
{
if ((checkCode != null) && !(checkCode.Trim() == string.Empty))
{
Bitmap image = new Bitmap((int)Math.Ceiling((double)(checkCode.Length * 12.5)), 0x14);
Graphics g = Graphics.FromImage(image);
try
{
int i;
Random random = new Random();
g.Clear(Color.White);
for (i = 0; i < 0x19; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, (float)2f, (float)2f);
for (i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}

/// <summary>
/// 隨機數
/// </summary>
/// <param name="length">長度</param>
/// <returns></returns>
private static string GetRandWord(int length)
{
string checkCode = string.Empty;
Random random = new Random();
for (int i = 0; i < length; i++)
{
char code;
int number = random.Next();
if ((number % 2) == 0)
{
code = (char)(0x30 + ((ushort)(number % 10)));
}
else
{
code = (char)(0x41 + ((ushort)(number % 0x1a)));
}
checkCode = checkCode + code.ToString();
}
return checkCode;
}

6、網頁設計中怎樣插入驗證碼

你參考下

http://.baidu.com/question/299956539.html

7、請問C語言驗證碼代碼怎麼打?

用strcmp函數比較兩個字元串,你上圖一個生成的字元串,另一個輸入的字元串,把這兩個傳遞到函數里,函數返回0就說明兩個字元串相等,輸入正確,反之輸入錯誤。(頭文件導入string.h)。

8、用C#編寫網頁怎麼生成驗證碼

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace News.Controllers
{
public class ImageCodeController : Controller
{
//
// GET: /ImageCode/
public ActionResult Index()
{
return View();
}

#region 生成圖片驗證碼

/// <summary>
/// 比較圖片中的字元和用戶輸入的字元
/// </summary>
/// <param name="uCode"></param>
/// <returns></returns>
public string GetCodeResult(string uCode)
{
string oldcode = Session["oldcode"] as string;
if (uCode.ToLower() == oldcode.ToLower())
{
return "yes";//用戶輸入正確
}
else
{
return "no";//用戶輸入錯誤
}
}

/// <summary>
/// 生成指定長度的字元串
/// </summary>
/// <returns></returns>
public ActionResult SecurityCode()
{
string code = CreateRandomCode(4); //驗證碼的字元為4個
Session["oldcode"] = code;//生成後,存在session中,用於和用戶輸入的值比較
return File(CreateValidateGraphic(code), "image/Jpeg");
}

/// <summary>
/// 生成隨機的字元串
/// </summary>
/// <param name="codeCount"></param>
/// <returns></returns>
public string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,a,b,c,d,e,f,g,h,i,g,k,l,m,n,o,p,q,r,F,G,H,I,G,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,s,t,u,v,w,x,y,z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}

/// <summary>
/// 創建驗證碼圖片
/// </summary>
/// <param name="validateCode"></param>
/// <returns></returns>
public byte[] CreateValidateGraphic(string validateCode)
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 16.0), 27);
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機生成器
Random random = new Random();
//清空圖片背景色
g.Clear(Color.White);
//畫圖片的干擾線
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
}
Font font = new Font("Arial", 13, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, 3, 2);

//畫圖片的前景干擾線
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//畫圖片的邊框線
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

//保存圖片數據
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);

//輸出圖片流
return stream.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
#endregion

}
}
----------------------控制器(上)------頁面(下)-------------------------------

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/js/jquery-1.7.2.min.js"></script>
</head>
<body>
<div>
<img src="/ImageCode/SecurityCode" id="imageclick" onclick="this.src=this.src+'?'" />
<input type="text" value="" id="uCode" />
<a href="javaScript:;" id="click">確定</a>
</div>
</body>
</html>

<script type="text/javascript">

$("#click").click(function () {
//用戶輸入
var uCode = $.trim($("#uCode").val());
//圖片中顯示的
//獲取點擊圖片後獲取的值,並和用戶輸入的值進行比較
$.post("/ImageCode/GetCodeResult", { uCode: uCode }, function (data) {
if (data == "yes") {
alert("yes");
} else {
alert("驗證碼輸入錯誤,請重新輸入");
//$("#imageclick").click();//錯誤後,自動點擊圖片,生成下一個驗證碼(可選)
}
});
});

</script>

9、怎樣實現輸入驗證碼呢?要用C語言的

#include <stdio.h>
#include <string.h>
main()
{
char a[15],i=3;
do{
printf("Password:");
gets(a);
if(strcmp(a,"hello")==0) break;//這里請連到你的密碼文件或者是連接到資料庫驗證,這里是 hello
}while(--i);
if(i==0) printf("Input errer 3 num.System close!\n");
else
{
//這里寫入你密碼正內確要執行容的代碼
}
}

與網頁設計怎麼加驗證碼用c相關的知識