Use php to implement website verification code function [recommended by bloggers] and php Verification Code

Source: Internet
Author: User

Use php to implement website verification code function [recommended by bloggers] and php 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 php gd2 graphics library support (in php. ini, search "php_gd2.dll" and find";Extension = php_gd2.dll "and remove the semicolon from 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 valueSession

Random 4-digit Verification Code

$ Check = rand );

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, 255,255,255, ImageColorAllocate ($ im ));

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, 100), ImageColorAllocate ($ img ));
$ Strx + = rand (15, 20 );

$ Stry = rand (2, 15 );
Imagestring ($ img, 5, $ strx, $ stry, substr ($ check, 781,117), ImageColorAllocate ($ img, 78 ));
$ Strx + = rand (15, 20 );

$ Stry = rand (2, 15 );
Imagestring ($ img, 5, $ strx, $ stry, substr ($ check,), ImageColorAllocate ($ img, 16, 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 ";}}

Indicate the source for reprinting.

Support me. Visit the author's personal website

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.