PHP Generate image Verification code, click to switch instances

Source: Internet
Author: User

Http://www.jb51.net/article/51506.htm

Now let's take a look at the PHP code

Copy CodeThe code is as follows:
<?php

Session_Start ();
function Random ($len) {
$srcstr = "1A2S3D4F5G6HJ8K9QWERTYUPZXCVBNM";
Mt_srand ();
$strs = "";
for ($i = 0; $i < $len; $i + +) {
$strs. = $srcstr [Mt_rand (0, 30)];
}
return $strs;
}

Randomly generated string
$STR = random (4);

Verify the width of the code picture
$width = 50;

Verify the height of the code picture
$height = 25;

Declare the picture format of the layer you want to create
@ Header ("Content-type:image/png");

Create a layer
$im = Imagecreate ($width, $height);

Background color
$back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF);

Blur Point Color
$pix = Imagecolorallocate ($im, 187, 230, 247);

Font Color
$font = Imagecolorallocate ($im, 41, 163, 238);

The point of painting blur function
Mt_srand ();
for ($i = 0; $i < $i + +) {
Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix);
}

Output character
Imagestring ($im, 5, 7, 5, $STR, $font);

Output Rectangle
Imagerectangle ($im, 0, 0, $width-1, $height-1, $font);

Output picture
Imagepng ($im);

Imagedestroy ($im);

$STR = MD5 ($STR);

Select a cookie
Setcookie ("Verification", $str, Time () + 7200, "/");

Select Session
$_session["verification"] = $STR;
?>

The next thing you can do is call it in the page:

Copy CodeThe code is as follows:

If you want to realize "can't see?" Change the "effect, add the following JS to the page

Copy CodeThe code is as follows:
function changing () {
document.getElementById (' Checkpic '). src= "/images/checkcode.php?" +math.random ();
}

PHP Generate image Verification code, click to switch instances

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.