| I read some articles about the verification code some time ago, that is, to generate an image with a string of randomly generated numbers or symbols, and add some interference pixels to the image to prevent OCR ), the user can identify the verification code information with the naked eye, and enter a form to submit the website for verification. A function can be used only after the verification is successful.
An article briefly introduces the implementation method as follows:
Code 1:
<? PHP
/*
* Filename: authpage. php
* Author: huuworm
* Date: 2003-04-28
* @ Copyleft huuworm.org
*/
Srand (double) microtime () * 1000000 );
// Verify that the user input is consistent with the verification code
If (isset ($ http_post_vars ['authinput'])
{
If (strcmp ($ http_post_vars ['authnum'], $ http_post_vars ['authinput']) = 0)
Echo "Verification Successful! ";
Else
Echo "Verification Failed! ";
}
// Generate a new four-digit integer Verification Code
While ($ authnum = rand () % 10000) <1000 );
?>
<Form action = authpage. php method = post>
<Table>
Enter the verification code: <input type = text name = authinput style = "width: 80px"> <br>
<Input type = submit name = "verify" value = "Submit verification code">
<Input type = hidden name = authnum value = <? Echo $ authnum;?>
</Table>
</Form>
Code 2:
<? PHP
/*
* Filename: authimg. php
* Author: huuworm
* Date: 2003-04-28
* @ Copyleft huuworm.org
*/
// Generate a verification code Image
Header ("Content-Type: image/PNG ");
Srand (double) microtime () * 1000000 );
$ Im = imagecreate (58,28 );
$ Black = imagecolorallocate ($ im, 0, 0 );
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Gray = imagecolorallocate ($ im, 200,200,200 );
Imagefill ($ im, 68, 30, $ gray );
// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 10, 8, $ http_get_vars ['authnum'], $ black );
For ($ I = 0; $ I <50; $ I ++) // Add interference pixels
{
Imagesetpixel ($ im, Rand () % 70, Rand () % 30, $ black );
}
Imagepng ($ IM );
Imagedestroy ($ IM );
?>
This program has basically implemented the verification code generation and verification function, but the author of this article does not know why the content of the verification code is displayed in the form. In this case, it only limits the user's need to enter the verification code, but does not prevent malicious programs. It can be said that it is difficult to protect people, rather than prevent attacks.
However, we can save the verification string in the session according to the original author's idea, so as to ensure security.
The Code is as follows:
// File: authform. php
<Script language = "JavaScript">
Function check (){
If (document. Forms [0]. Num. value = ''){
Alert ("Enter the Verification Code ");
Return false;
Exit;
}
Return true;
}
</SCRIPT>
<Form action = authpage. php method = post onsubmit = "Return check ();">
<Table>
Enter the verification code: <input type = text name = num style = "width: 80px"> <br>
<Input type = submit name = "verify" value = "Submit verification code">
</Table>
</Form>
<? PHP
/*
* Filename: authimg. php
*/
Header ("Content-Type: image/PNG ");
Session_start ();
$ Auth_num = "";
Session_register ('auth _ num ');
$ Im = imagecreate (63,20 );
Srand (double) microtime () * 1000000 );
$ Auth_num_k = MD5 (RAND (0, 9999 ));
$ Auth_num = substr ($ auth_num_k, 17,5 );
$ Black = imagecolorallocate ($ im, 0, 0 );
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Gray = imagecolorallocate ($ im, 200,200,200 );
// Imagefill ($ im, $ black); // This line does not know why an error occurs on the server of our company. Change the space to OK.
Imagestring ($ im, 5, 10, 3, $ auth_num, $ gray );
For ($ I = 0; I I <200; $ I ++)
{
$ Randcolor = imagecolorallocate ($ im, Rand (0,255), Rand (0,255), Rand (0,255 ));
Imagesetpixel ($ im, Rand () % 70, Rand () % 30, $ randcolor );
}
Imagepng ($ IM );
Imagedestroy ($ IM );
?>
<? PHP
/*
* Filename: authpage. php
*/
Session_start ();
$ Num = trim ($ num );
If ($ auth_num ==$ num & $ num <> ""){
Echo "Verification Successful ";
} Else {
Echo "Verification Failed ";
}
?>
I am also very depressed when writing this article. I found only that article by using "Verification code + PHP" on Google, which means I cannot find a practical point. No way. Although the foreign language level is normal, I still use a full English search to find a piece of code written by programmers in a country that I don't know. I changed the variable to a form that is easy to understand by Chinese people, this is what I did. I really hope that I can search for some in-depth articles and tutorials in Chinese next time, not to advocate resource sharing ...... |