PHP Generate authentication Code source code

Source: Internet
Author: User
Tags rand strlen


Session_Start ();

$ENABLEGD = 1;
$funcs = Array (' Imagecreatetruecolor ', ' imagecolorallocate ', ' Imagefill ', ' imagestring ', ' imageline ', ' imagerotate ', ' Imagedestroy ', ' imagecolorallocatealpha ', ' imageellipse ', ' imagepng ');
foreach ($funcs as $func)
{
if (!function_exists ($func))
{
$ENABLEGD = 0;
Break
}
}

Ob_clean ();

if ($ENABLEGD)
{
 //create captcha
  $consts = ' cdfgkmnpqrstwxyz23456 ';
  $vowels = ' aek23456789 ';
 for ($x = 0; $x < 6 $x + +)
 {
   $const [$x] = substr ($consts, Mt_rand (0,strlen ($consts )-1, 1);
   $vow [$x] = substr ($vowels, Mt_rand (0,strlen ($vowels)-1), 1);
 }
  $radomstring = $const [0]. $vow [0]. $const [2]. $const [1]. $vow [1]. $const [3]. $vow [3]. $const [4];
 $_session[' checkcode '] = $string = substr ($radomstring, 0,4);//only Display 4 str
 //set up image, the The the width and the second is the height
  $imageX = strlen ($radomstring) *8; //the image width
  $imageY = 20;      //the Image Height
  $im = Imagecreatetruecolor ($ ImageX, $imageY);

 //creates two variables to store color
  $background = Imagecolorallocate ($im, rand (180,), Rand (180, ), rand (180, 250));
  $FOREGROUNDARR = Array (imagecolorallocate ($im, rand (0), rand (0), rand (0)),
          imagecolorallocate ($im, rand (0), rand (0, ten), rand (245, 255)),
          imagecolorallocate ($im, rand (245, 255), rand (0), rand (0,),
          imagecolorallocate ($im, rand (245, 255), rand (0), rand (245, 255));
  $foreground 2 = Imagecolorallocatealpha ($im, Rand (a), Rand (a), Rand (20, 100), 80);
  $middleground = Imagecolorallocate ($im, rand (160), rand (160), Rand (200, 160));
  $middleground 2 = Imagecolorallocatealpha ($im, rand (180, 140), Rand (180, 140), Rand (180, 140), n);

Fill image with bgcolor
Imagefill ($im, 0, 0, imagecolorallocate ($im, 250, 253, 254));
Writes string
Imagettftext ($im, Rand ( -30), 5, Rand (a), $foregroundArr [Rand (0,3)], phpcms_root. ' Include/fonts/alger. TTF ', $string [0]);
Imagettftext ($im, Rand ( -50), Rand (a), $foregroundArr [Rand (0,3)], phpcms_root. ' Include/fonts/arialni. TTF ', $string [1]);
Imagettftext ($im, Rand ( -50), Rand (a), $foregroundArr [Rand (0,3)], phpcms_root. ' Include/fonts/alger. TTF ', $string [2]);
Imagettftext ($im, Rand ( -30), Rand (a), $foregroundArr [Rand (0,3)], phpcms_root. ' Include/fonts/arial.ttf ', $string [3]);

Strikethrough

$border = Imagecolorallocate ($im, 133, 153, 193);
Imagefilledrectangle ($aimg, 0, 0, $x _size-1, $y _size-1, $back);
Imagerectangle ($im, 0, 0, $imageX-1, $imageY-1, $border);

$pointcol = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
For ($i =0 $i <80; $i + +)
{
Imagesetpixel ($im, rand (2, $imageX-2), Rand (2, $imageX-2), $pointcol);
}
Random shapes
For ($x =0 $x <9; $x + +)
{
if (Mt_rand (0, $x)%2==0)
{
Imageline ($im, rand (0), rand (0,), rand (0), rand (0,), rand (0, 999999));
Imageellipse ($im, rand (0), rand (0,), rand (0), rand (0,), $middleground 2);
}
Else
{
Imageline ($im, rand (0), rand (0,), rand (0), rand (0,), rand (0, 999999));
Imageellipse ($im, rand (0), rand (0,), rand (0), rand (0,), $middleground);
}
}
Output to Browser
Header ("Content-type:image/pngrn");
Imagepng ($im);
Imagedestroy ($im);
}
Else
{
$files = Glob (phpcms_root. ' Images/checkcode/*.jpg ');
if (!is_array ($files)) exit ($LANG [' Please_check_dir_images_checkcode ']);

$checkcodefile = $files [Rand (0, COUNT ($files)-1)];
$_session[' checkcode '] = substr (basename ($checkcodefile), 0, 4);

Header ("Content-type:image/jpegrn");
Include $checkcodefile;
}

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.