Home  >  Article  >  Backend Development  >  PHP verification code code example_PHP tutorial

PHP verification code code example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:38:47937browse

When we write user verification pages, such as registration and login, in order to enhance the security of user login, we add verification code verification.
The verification code generates a PNG image through GD, assigns the $randval random number to $_SESSION[login_check_num], and compares it with the $_POST input by the user to determine whether it is correct. To achieve the functions that need to be implemented, you need to modify the php.ini file so that php supports the GD library.
//Calling this page, if the following formula is true, generate a verification code image
if($_GET["action"]=="verifycode")
{
rand_create();
}
//Verification code image generation
function rand_create()
{
//Notify the browser that a PNG image will be output
Header("Content-type: image/PNG");
//Get ready the random number generator seed
srand((double)microtime()*1000000);
//Prepare the relevant parameters of the image
$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0); //RGB black identifier
$white = ImageColorAllocate($im, 255,255,255); //RGB white identifier
$gray = ImageColorAllocate($im, 200,200,200); //RGB gray identifier
//Start drawing
Imagefill($im,0,0,$gray);
While(($randval=rand()%100000)<10000);{
          $_SESSION["login_check_num"] = $randval;
//Plot the four-digit integer verification code into the picture
Imagestring($im, 5, 10, 3, $randval, $black);
}
//Add interference pixels
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);
}
//Output verification image
ImagePNG($im);
//Destroy image identifier
ImageDestroy($im);
}
//Check verification code
function rand_check()
{
If($_POST["reg_rand"] == $_SESSION["login_check_num"]){
         return true;
}
else{
exit("Verification code input error");
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486454.htmlTechArticleWhen we write user verification pages, such as registration and login, in order to enhance the security of user login, we add verification code verification. The verification code generates a PNG image through GD and randomizes $randval...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn