Joke Collection Website - Public benefit messages - Seek to use program code to realize the code of sending short messages to mobile phones, preferably in c# language. Net or php.

Seek to use program code to realize the code of sending short messages to mobile phones, preferably in c# language. Net or php.

When designing user login module, we often use verification code, which can effectively prevent malicious cracking of hacker software. Now open the source code of my commonly used verification code.

How to use:

1. Add a class in the Web project, such as "CreateImage.cs", and then copy the source code I published;

2. Create a new Web form, such as "Image.aspx", and add the code "CreateImage". DrawImage () "to Page_Load. Of course, don't forget to add a reference to the class! !

3. Add the following javascript code to the appropriate position on the page (where you want to put the verification code).

The source code is as follows:

[csharp] view plain text

///

///Verification code module

///

Public class creation image

{

Public static void DrawImage ()

{

create image img = new create image();

HttpContext。 current . session[" check code "]= img。 rnd num(4);

img。 Create an image (HttpContext. Current session ["CheckCode"]. ToString());

}

///

///Generate a verification picture

///

///Verification character

Private void CreateImages (string check code)

{

int iwidth = (int)(checkCode。 Length *13);

System. Drawing. Bitmap image = new system. Drawing.Bitmap(iwidth,23);

Graph g = graph. FromImage;

G. transparent (color. White);

//Define color

Color[] c = {Color。 Black, color. Red, color. Dark blue, color. Green, color. Orange, color. Brown, color. Dark cyan, color. Purple};

//Define the font

String [] font = {"verdana "," Microsoft sans serif "," Comic sans ms "," Arial "," Song Ti " };

Random rand = new Random();

//Random output noise

for(int I = 0; I & lt50; i++)

{

Int x = rand. Next (picture. Width);

Int y = rand. Next (picture. Height);

G.DrawRectangle (new pen (color. Light gray, 0), x, y, 1,1);

}

//Output verification code characters with different fonts and colors.

for(int I = 0; I

{

int cindex = rand。 Next (7);

int findex = rand。 Next (5);

Font f = new system. Drawing.Font (font [findex], 10, system. drawing . font style . bold);

Brush b = new system. drawing . solid brush(c[cindex]);

int ii = 4;

if((i+ 1)%2==0)

{

ii = 2;

}

G. Pull the rope (check code. Substring (i, 1), f, b, 3+(i* 12), ii);

}

//Draw a border

G.DrawRectangle (new pen (color. Black, 0), 0, 0, image. Width-1, image. Height-1);

//output to browser

System. IO.MemoryStream ms = new system. IO . memory stream();

Image. Save (ms, system. drawing . imaging . image format . JPEG);

HttpContext。 current . response . clear content();

//response. clear content();

HttpContext。 current . response . content type = " image/Jpeg ";

HttpContext。 current . response . binary write(ms . to array());

g.dispose();

Image. dispose();

}

///

///Generate random letters

///

///Number of letters generated

///String

Private string RndNum(int VcodeNum)

{

string Vchar = "0, 1,2,3,4,5,6,7,8,9 ";

string[] VcArray = Vchar。 Split (',');

String VNum =// Because the string is short, StringBuilder is not needed.

int temp =- 1; //Record the last random number and try to avoid generating several identical random numbers.

//Adopt a simple algorithm to ensure the difference of generated random numbers.

Random rand = new Random();

for(int I = 1; I & ltvcodenum+1; i++)

{

if ( temp! = - 1)

{

rand = new Random(I * temp * unchecked((int)DateTime。 now . Ticks));

}

Int t = rand. Next (VcArray. Length);

if (temp! =- 1 & amp; & temperature == t)

{

Returns rndnum (vcodenum);

}

temp = t;

VNum+= VC array[t];

}

Return to VNum

}

}