PHP Simple Verification Code Generation instance

Source: Internet
Author: User


In the site landing and registration, often used to verify the code to prevent other people with mechanical violence registration or landing, plus the verification code to a certain extent to make the site a lot of security, the following is a relatively simple verification code generation, while assigning the session value.

The code is as follows Copy Code

<?php
Session_Start ();
Header ("Content-type:image/png");
Create a true Color white paper
$im = @imagecreatetruecolor (die) or ("Create image Failed");
Get background color
$background _color = imagecolorallocate ($im, 255, 255, 255);
Fill background color (this thing is similar to oil drums)
Imagefill ($im, 0,0, $background _color);
Get border color
$border _color = imagecolorallocate ($im, 200,200,200);
Draw a rectangle, border color 200,200,200
Imagerectangle ($im, 0,0,49,19, $border _color);

Show off the background by line, full screen with 1 or 0
For ($i =2 $i <18; $i + +) {
Get random tint
$line _color = Imagecolorallocate ($im, Rand (200,255), Rand (200,255), Rand (200,255));
Draw Line
Imageline ($im, 2, $i, $i, $line _color);
}

Set Font size
$font _size=12;

Set up the printed text
$Str [0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$STR [1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$STR [2] = "01234567891234567890123456″;

Get 1th Random text
$imstr [0]["s"] = $Str [rand (0,2)][rand (0,25)];
$IMSTR [0]["x"] = rand (2,5);
$imstr [0]["y"] = rand (1,4);

Get 2nd random Text
$imstr [1]["s"] = $Str [rand (0,2)][rand (0,25)];
$IMSTR [1]["x"] = $imstr [0]["x"]+ $font _size-1+rand (0,1);
$imstr [1]["y"] = rand (1,3);

Get 3rd random text
$imstr [2]["s"] = $Str [rand (0,2)][rand (0,25)];
$IMSTR [2]["x"] = $imstr [1]["x"]+ $font _size-1+rand (0,1);
$imstr [2]["y"] = rand (1,4);

Get 4th Random Text
$imstr [3]["s"] = $Str [rand (0,2)][rand (0,25)];
$IMSTR [3]["x"] = $imstr [2]["x"]+ $font _size-1+rand (0,1);
$imstr [3]["y"] = rand (1,3);

Assign the displayed array to the session
$_session[' CODE ' = $imstr [0]["S"]. $imstr [1]["S"]. $imstr [2]["S"]. $imstr [3]["S"];

//write Random Strings
for ($i =0 $i <4; $i + +) {
$text _color = imagecolorallocate ($im, Rand (50,180), Rand (50,180), Rand ( 50,180));
Imagechar ($im, $font _size, $imstr [$i] ["X"], $imstr [$i] ["Y"], $imstr [$i] ["s"], $text _color);
}
///Show Picture
Imagepng ($im);
//Destroy picture
Imagedestroy ($im);

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.