Implement verification code in PHP development process _php Tutorial

Source: Internet
Author: User
Some time ago read some of the verification code of the article, that is, a series of randomly generated numbers or symbols, create a picture, the picture with some interference pixels (to prevent OCR), by the user's naked eye identification of the verification code information, the input form submitted to the site verification, verification success to use a function.
  
An article also briefly describes the implementation of the method, as follows:
  
   Code One:
     
/*
  
* Filename:authpage.php (as the current mainstream development language)
  
* Author:hutuworm
  
* date:2003-04-28
  
* @Copyleft hutuworm.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 "Verified success! ";
  
Else
  
echo "Verification failed! ";
  
}
  
Generate a new four-bit integer verification code
  
while (($authnum =rand ()%10000) <1000);
  
?>
  
  
  
   Code two:
     
/*
  
* Filename:authimg.php (as the current mainstream development language)
  
* Author:hutuworm
  
* date:2003-04-28
  
* @Copyleft hutuworm.org
  
*/
  
Generate a Captcha 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);
  
Drawing a four-bit integer verification code into a picture
  
Imagestring ($im, 5, 8, $HTTP _get_vars[authnum], $black);
  
for ($i =0; $i <50; $i + +)//Add interfering pixels
  
{
  
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $black);
  
}
  
Imagepng ($im);
  
Imagedestroy ($im);
  
?>
  
This program has basically achieved the verification code generation and verification function, but the article author does not know why the content of the verification code is displayed in the form, so that, only restricts the user must enter the verification code, the malicious program has no preventive effect. Can be said to be difficult for people, rather than to guard against attacks.
  
But fortunately, according to the original author's ideas, we can save the validation string in the session, so that it has a certain degree of security.
  
   The code is as follows:
File:authform.php (as the current mainstream development language)
  
  
  
  
  
     
/*
  
* "Filename:authimg.php (as the current mainstream development language)
  
*/
  
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,0);
  
$white =imagecolorallocate ($im, 255,255,255);
  
$gray =imagecolorallocate ($im, 200,200,200);
  
Imagefill ($im, 63,20, $black);//This line does not know why in my company's server error, change a space ok
  
Imagestring ($im, 5,10,3, $auth _num, $gray);
  
for ($i =0; $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);
  
?>
  
     
/*
  
* Filename:authpage.php (as the current mainstream development language)

http://www.bkjia.com/PHPjc/508612.html www.bkjia.com true http://www.bkjia.com/PHPjc/508612.html techarticle some time ago read some of the verification code of the article, that is, a series of randomly generated numbers or symbols, create a picture, the picture with some interference pixels (to prevent OCR), by the user meat ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.