PHP Entry level PHP Verification Code Program

Source: Internet
Author: User

Instance

The code is as follows Copy Code

<?php 
Session_ Start (); 
 
$im = imagecreate (80,30);//Create picture  
$color = Imagecolorallocate ($im, rand (150,200) , Rand (150,200), Rand (150,200))//Set picture background  
$str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789 "//Generate random string  
 for ($i =0; $i <5; $i + +) { 
        $code. = $str [Rand (0, (strlen ($STR)-1)]; 
        } 
$_session[' code ' = $code; 
for ($a =0; $a <5; $a + +) {  Write string to picture resource  
    $x = $a *10 + 15; 
    $y = rand (5,10); //www.11 1cn.net
    Imagechar ($im, 5, $x, $y, $code {$a},imagecolorallocate ($im, 0,0,0)); 
    } 
Header ("content-type:image/png")//Output Picture resource  
Imagepng ($im); 

Example 2

The code is as follows Copy Code

<?php
if (!isset ($_session)) {//To determine whether the session is open
Session_Start (); Open on the session
}
$width = 70; width of cloth painting
$height = 25; Cloth painting Height
$length =4;//Verification Code length
$code =getcode ($length); Get a random string
$_session[' verfycode ' = $code;

$img =imagecreate ($width, $height);
$bgcolor =imagecolorallocate ($img, 240,240,240);
$rectangelcolor =imagecolorallocate ($img, 150,150,150);
Imagerectangle ($img, 1,1, $width-1, $height-1, $rectangelcolor);//Draw Border
for ($i =0; $i < $length; $i + +) {//looping writing
$codecolor =imagecolorallocate ($img, Mt_rand (50,200), Mt_rand (50,128), Mt_rand (50,200));
$angle =rand ( -20,20);
$charx = $i *15+8;
$chary = ($height +14)/2+rand ( -1,1);
Imagettftext ($img, $angle, $charx, $chary, $codecolor, ' C:windowsfontssimkai.ttf ',
$code [$i]);
}
For ($i =0 $i <20; $i + +) {//Looping line drawing
$linecolor =imagecolorallocate ($img, Mt_rand (0,250), Mt_rand (0,250), Mt_rand (0,250));
$linex =mt_rand (1, $width-1);
$liney =mt_rand (1, $height-1);
Imageline ($img, $linex, $liney, $linex +mt_rand (0,4)-2, $liney +mt_rand (0,4)-2, $linecolor);
}
For ($i =0 $i <100; $i + +) {//Loop point
$pointcolor =imagecolorallocate ($img, Mt_rand (0,250), Mt_rand (0,250), Mt_rand (0,250));
Imagesetpixel ($img, Mt_rand (1, $width-1), Mt_rand (1, $height-1), $pointcolor);
}
function GetCode ($length) {//Generate PHP random number
$pattern = ' 1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';//character Fu Chi
for ($i =0; $i < $length; $i + +) {
$key. = $pattern {Mt_rand (0,35)};
}
return $key;

}
Ob_clean ();
Header (' content-type:image/png ');
Imagepng ($IMG);
?>

The effect chart is as follows

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.