PHP Verification Code Implementation code simple example

Source: Internet
Author: User
Tags rand

Recently bored nothing to do, began to do some of the sharing of PHP, the code is relatively simple, but more practical.

This is written four years ago, a few lines of code to achieve four-bit random number of PHP verification code function.

  code is as follows copy code

Session_Start ();
Header (" Content-type:image/jpeg ");
$img = Imagecreate (50,20);
$white = imagecolorallocate ($img, 211,213,193);
Imagefill ($img, 0,0, $white); <br <p>for ($i =0; $i <4; $i + +) {
$r [$i] = rand (0,9);
}
$_session[' valid '] = Implode ("", $r); Writing here to the session can be done late validation
for ($i =0 $i <4; $i + +) {

if (rand (0,9)%2==0) {
Imagechar ($img, 5, ($i + 1) *8,4, $r [$ I],imagecolorallocate ($img, Rand (0,150), Rand (0,150), Rand (0,150));
}else{
Imagechar ($img, 5, ($i + 1) *8,2, $r [$i],imagecolorallocate ($img, Rand (0,150), Rand (0,150), Rand (0,150))) ;
}

}
imagejpeg ($img);

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.