Tutorials to .com

Tutorials to .com » Php » Others » PHP code used to achieve functional

PHP code used to achieve functional

Print View , by: iSee ,Total views: 7 ,Word Count: 693 ,Date: Mon, 20 Apr 2009 Time: 12:04 AM


  Currently, many sites make use of robots in order to prevent automatic registration of users, logging, irrigation, use of
Authentication code. The so-called verification code is a string of randomly generated numbers or symbols to generate a picture,
Interference with a number of picture in pixels (Prevention OCR), the naked eye by the user identification information of the authentication code, lose
Verification form to submit your site to verify the success of a feature to use.

  showed us how to write php program to achieve functional verification code:

  A code:

<? php
/ *
* Filename: authpage.php
* Author: hutuworm
* Date: 2003-04-28
* @ Copyleft hutuworm.org
* /

srand ((double) microtime () * 1000000);

/ / Validate user input and validation code is the same
if (isset ($ HTTP_POST_VARS [ 'authinput']))
(
if (strcmp ($ HTTP_POST_VARS [ 'authnum'], $ HTTP_POST_VARS [ 'authinput'])== 0)
echo "Authentication successful!";
else
echo "Authentication failed!";
)

/ / Generate a new code of the four integer
while (($ authnum = rand ()% 10000) <1000);
?>
<form action=authpage.php method=post>
<table>
Please enter verification code: <input type=text name=authinput style="width: 80px"> <br>
<input type=submit name="" value="">
<input type = hidden name = authnum value = <? echo $ authnum;?>>
<img src = authimg.php? authnum = <? echo $ authnum;?>>
</ table>
</ form>

  code 2:

<? php
/ *
* Filename: authimg.php
* Author: hutuworm
* Date: 2003-04-28
* @ Copyleft hutuworm.org
* /

/ / Generate a verification code picture
Header ( "Content-type: image / PNG");
srand ((double) microtime () * 1000000);
$ im = imagecreate (58,28);
$ black = ImageColorAllocate ($ im, 0,0,0);
$ white = ImageColorAllocate ($ im, 255,255,255);
$ gray = ImageColorAllocate ($ im, 200,200,200);
imagefill ($ im, 68,30, $ gray);

/ / Will draw the four integer code into the picture
imagestring ($ im, 5, 10, 8, $ HTTP_GET_VARS [ 'authnum'], $ black);

for ($ i = 0; $ i <50; $ i + +) / / join the interference pixel
(
imagesetpixel ($ im, rand ()% 70, rand ()% 30, $ black);
)

ImagePNG ($ im);
ImageDestroy ($ im);
 ?>



  In this paper, the procedures in the Apache 2.0.45 + PHP 4.3.1 to run through the environment.

Only on the verification code above, function of the realization of a simple and does not take commercial security issues. To enhance security, this functionality into business applications, you can achieve through the following steps:

1. The opening of Session.
2. Authnum generated in authimg.php and calculate md5sum, into the session.
3. Authpage.php will authinput after calculating the md5sum with the session of authnum (md5sum) to verify the results of comparison to draw.


Site Note: The authors use a simple code to achieve a cool feature. But when you add the effect of interference pixels is not too good, you can log on to look at the forum when the rain-tested code (http://ror.cn/perl/ut/user_login.cgi), even a little change to the second paragraph of the code then, instead of generating a similar effect.

The revised code is as follows:

<? php
/ *
* Filename: authimg.php
* Author: hutuworm
* Date: 2003-04-28
* @ Copyleft hutuworm.org
* /
/ / Generate a verification code picture
Header ( "Content-type: image / PNG");
srand ((double) microtime () * 1000000);
$ im = imagecreate (62,20);
$ black = ImageColorAllocate ($ im, 0,0,0);
$ white = ImageColorAllocate ($ im, 255,255,255);
$ gray = ImageColorAllocate ($ im, 200,200,200);
imagefill ($ im, 68,30, $ gray);
while (($ authnum = rand ()% 100000) <10000);
/ / Will draw the four integer code into the picture
imagestring ($ im, 5, 10, 3, $ authnum, $ black);
for ($ i = 0; $ i <200; $ i + +) / / join the interference pixel
(
$ randcolor = ImageColorallocate ($ im, rand (0255), rand (0255), rand (0255));
imagesetpixel ($ im, rand ()% 70, rand ()% 30, $ randcolor);
)
ImagePNG ($ im);
ImageDestroy ($ im);
?>
The result will be displayed as follows:



Friends who are interested can try.



php other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.