Php verification code generation tutorial

Source: Internet
Author: User

This article provides a detailed tutorial on generating a verification code for a php Tutorial. The previous section describes how to generate and call a verification core code.

The code is as follows: Copy code

<? Php
// Header ("content-type: image/png ");
$ Num = '000000 ';
$ Imagewidth = 60;
$ Imageheight = 18;

$ Numimage = imagecreate ($ imagewidth, $ imageheight );
Imagecolorallocate ($ numimage, 240,240,240 );
For ($ I = 0; $ I <strlen ($ num); $ I ++ ){
$ X = mt_rand (1, 8) + $ imagewidth * $ I/4;
$ Y = mt_rand (1, $ imageheight/4 );
$ Color = imagecolorallocate ($ numimage, mt_rand (0,150), mt_rand (0,150), mt_rand (0,150 ));
Imagestring ($ numimage, 5, $ x, $ y, $ num [$ I], $ color );
}

For ($ I = 0; I I <200; $ I ++ ){
$ Randcolor = imagecolorallocate ($ numimage, rand (200,255), rand (200,255), rand (200,255 ));
Imagesetpixel ($ numimage, rand () % 70, rand () % 20, $ randcolor );
}
Imagepng ($ numimage );
Imagedestroy ($ numimage );
?>


Let's look at an instance that generates a verification code.

The code is as follows: Copy code

<? Php

// Generate a verification code Image

Session_start ();

Header ("content-type: image/png ");

Srand (double) microtime () * 1000000 );

$ Roundnum = rand );

// Store the random number in the session for future use

$ _ Session ["sessionround"] = $ roundnum;

$ Im = imagecreate (58,28 );

$ Red = imagecolorallocate ($ im, 255, 0, 0 );

$ Blue = imagecolorallocate ($ im, 0,255, 0 );

// Fill in the local area, which is equivalent to the background

Imagefill ($ im, 68, 30, $ red );

// Print the four-digit integer verification code into the image

Imagestring ($ im, 5, 10, 8, $ roundnum, $ blue );

For ($ I = 0; $ I <50; $ I ++) // add interference pixels

        {

Imagesetpixel ($ im, rand () % 70, rand () % 30, $ black );

        }

Imagepng ($ im );

Imagedestroy ($ im );

?>


Html call method

The code is as follows: Copy code

<Td> verification code </td> <td width = "20%">

<Input name = "validt" type = "text" size = "10" maxlength = "4" type = "codeph" text = "codeph"/> </td> <td colspan = "3" width = "80%">

</td>

 

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.