PHP implementation of simple verification code function

Source: Internet
Author: User

1. According to the GD Library in PHPPicture to process, draw a picture of the verification code, code.php
2. Form interface, simple session save and compare with user input, determine whether the validation is correct, form.php

<?PHP Session_Start();//must be at the top of the script $image=imagecreatetruecolor (100, 30);//Imagecreatetruecolor function to build a true color image//Generate color pixels $bgcolor=imagecolorallocate ($image, 255, 255, 255);//White background imagecolorallocate function assigns color to an image $textcolor=imagecolorallocate ($image, 0,0,255);//Blue text//fill function, XY determines coordinates, color color performs area fill colorImagefill ($image, 0, 0,$bgcolor); $captch _code="";//Initial empty value//the loop, loop fetch number for($i= 0;$i<4;$i++){ $fontsize=6; $x=($i*25) +Rand(5,10); $y=Rand(5,10);//position random//$fontcontent = $i >2?chr (rand (97,122)): Chr (rand (65,90));//is lowercase, otherwise uppercase $data= ' abcdefghijkmnpqrstuvwxyz3456789 '; $fontcontent=substr($data,Rand(0,strlen($data)-1), 1);//strlen is just a counter work with numbers and letters of verification code//can be understood for an array length of 0 to
$fontcolor=imagecolorallocate($image,Rand(0, -),Rand( 0,+),rand(0 ,+)); ///random RGB () value can be determined by itself
Imagestring ($image,$fontsize,$x,$y,$fontcontent,$fontcolor);//to draw a line of strings horizontally    $captch _code.=$fontcontent;} $_session[' Authcode ']=$captch _code;//Save the variable in the Authcode variable of the session//the loop, cycle the point of the background interference     for($m= 0;$m<=600;$m++){     $x 2=Rand(1,99); $y 2=Rand(1,99); $pointcolor=imagecolorallocate ($image,Rand(0,255),Rand(0,255),Rand(0,255)); Imagesetpixel ($image,$x 2,$y 2,$pointcolor);//to draw a sequence of dots horizontally    }    //the loop, the loop draws the interfering line    for($i= 0;$i<=10;$i++){       $x 1=Rand(0,99); $y 1=Rand(0,99); $x 2=Rand(0,99); $y 2=Rand(0,99); $linecolor=imagecolorallocate ($image,Rand(0,255),Rand(0,255),Rand(0,255)); Imageline ($image,$x 1,$y 1,$x 2,$y 2,$linecolor);//Draw a line segment      }   Header(' Content-type:image/png '); Imagepng ($image); //destroyedImagedestroy ($image);?>


<-------------------------------- code.php---------------------------End----------------------------------------->
<?PHP//session save value and match user input value    if(isset($_request[' Authcode '])) {        Session_Start(); if(Strtolower($_request[' Authcode ']) ==$_session[' Authcode ']) {//Strtolower converted to lowercase functions            Echo"Input correct!" "; #code ...        }        Else{            Echo"Input Error! "; }        Exit(); }? ><! DOCTYPE html>

<---------------------------------form.php ---------------End of form interface---------------------------------------------->

PHP implements a simple captcha function

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.