PHP entry-level PHP Verification Code Program _php Tutorial

Source: Internet
Author: User
Generate verification code we will use PHP graphics processing functions, such as imagecreate,imagepng,header functions, let's look at a simple example.

Instance

The code is as follows Copy Code

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 + +) {//writes a string to a picture resource
$x = $a *10 + 15;
$y = rand (5,10); www.bKjia.c0m
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

if (!isset ($_session)) {//Determine if 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 random string
$_session[' verfycode '] = $code;

$img =imagecreate ($width, $height);
$bgcolor =imagecolorallocate ($img, 240,240,240);
$rectangelcolor =imagecolorallocate ($img, 150,150,150);
Imagerectangle ($img, $width-1, $height-1, $rectangelcolor);//Draw Border
for ($i =0; $i < $length; $i + +) {//circular 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 + +) {//Loop draw line
$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 draw 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 descriptors
for ($i =0; $i < $length; $i + +) {
$key. = $pattern {Mt_rand (0,35)};
}
return $key;

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

As follows

http://www.bkjia.com/PHPjc/632615.html www.bkjia.com true http://www.bkjia.com/PHPjc/632615.html techarticle Generate verification Code we will use PHP graphics processing functions, such as imagecreate,imagepng,header functions, let's look at a simple example. The instance code is copied code 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.