PHP Verification code--character

Source: Internet
Author: User

The production of character verification code is divided into five steps:

1. Generating images

2. Generate Random Numbers

3. Making interference points, lines

4. Output image

5. Destroying images


Directly below the code

Server-side:

<?phpsession_start (); $width = n; $length = n; $captch _code = "; $image = Imagecreatetruecolor ($width, $length); $ color = imagecolorallocate ($image, 255, 255, 255), Imagefill ($image, 0, 0, $color);//Generate random number for ($i =0; $i <4; $i + +) {$ FontSize = 6; ? $fontcolor = Imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120)); $data = " abcdefghijklmnopqrstuvwxyz1234567890 "; $fontcontent = substr ($data, Rand (0,strlen ($DATA)), 1); $captch _code. = $ fontcontent; $x = ($i * $width/4) + rand ($length/6, $length/3); $y = rand ($length/6, $length/3); Imagestring ($image, $ FontSize, $x, $y, $fontcontent, $fontcolor);} $_session[' authcode ' = $captch _code;//interference point for ($i =0; $i <200; $i + +) {$pointcolor = Imagecolorallocate ($image, Rand ( 50,200), Rand (50,200), Rand (50,200)), Imagesetpixel ($image, rand (1, $width-1), rand (1, $width-1), $pointcolor);} Interference line for ($i =0; $i <5; $i + +) {$linecolor = Imagecolorallocate ($image, Rand (80,220), Rand (80,220), Rand (80,220)); Imageline ($image, rand (1, $width-1), rand (1, $width/3-1), rand (1,$width-1), rand (1, $width/3-1), $linecolor);} Header ("Content-type:image/png"); Imagepng ($image); Imagedestroy ($image);


Client validation Code:

<?phpif (Isset ($_request[' Authcode ')) {session_start (); if (Strtolower ($_request[' authcode ']) = = $_session[' Authcode '] {echo ' <font color= "#0000cc" > Input correct </font> ';} Else{echo ' <font color= "#cc0000" ><b> input error </b></font> ';} Exit ();}? ><! DOCTYPE html>


Result diagram:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP Verification code--character

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.