Use php to implement the website verification code function [recommended] and php to implement the website Verification Code
Verification code is a common security measure for websites and a skill that is difficult for new webmasters. Here I will introduce you to a simple and effective verification code implementation method.
Before getting started
Before the official start, we need to open the support for the php gd2 graphics library (in php. ini, search "php_gd2.dll", find "; extension = php_gd2.dll", and remove the semicolon at the beginning of the sentence ).
You can refer to: how to open the php gd2 Library
Core: img. php
This page generates a verification code and writes the correct value to the Session.
Random 4-digit Verification Code
$check=rand(1000,9999);
Write the generated verification code to the session
Session_start(); $_SESSION["check"] = $check;
Create an image
$im = imagecreate(80,30);
Because the background of this image is black by default, we need to fill it with white.
imagefill($im,0,0,ImageColorAllocate($im, 255,255,255));
Use imageline to randomly draw two solid lines
$y1=rand(0,30); $y2=rand(0,30); $y3=rand(0,30); $y4=rand(0,30); imageline($im,0,$y1,70, $y3,000); imageline($im,0,$y2,70, $y4,000);
Draw text at random locations
$strx=rand(3,15); $stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10));
Output Image
Header("Content-type: image/PNG"); ImagePNG($img);
Complete code
<?php $check=rand(1000,9999);Session_start(); $_SESSION["check"] = $check; $img = imagecreate(80,30); imagefill($img,0,0,ImageColorAllocate($img,255,255,255)); $y1=rand(0,30); $y2=rand(0,30); $y3=rand(0,30); $y4=rand(0,30); imageline($img,0,$y1,70, $y3,ImageColorAllocate($img,55,255,25)); imageline($img,0,$y2,70, $y4,ImageColorAllocate($img,55,55,255)); $strx=rand(3,15); $stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10)); Header("Content-type: image/PNG"); ImagePNG($img);
User Interface: index. php
I think everyone knows how to do this. I will give the code directly.
<! DOCTYPE html> <body> <form action = "action. php "method =" post "> <input type =" text "name =" cikle "placeholder =" Verification Code "> <br> <input type =" submit "value =" Submit "> </form> </body>
The above code passes the value entered by the user to "action. php ".
Check: action. php
In this step, we need to compare the user input value with the value in the session.
Equal, the output is "correct"
Not equal. The output is "Incorrect"
<? PhpSession_start (); if ($ _ SERVER ["REQUEST_METHOD"] = "POST") {if ($ _ SESSION ["check"]! = Intval ($ _ POST ["cikle"]) {echo "Incorrect" ;}else {echo "correct ";}}
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!