Example analysis of PHP imitation QQ verification Code

Source: Internet
Author: User
Tags set background
This article introduces a PHP implementation of imitation QQ in the example of verification code, QQ registration or login Verification code, we have seen it, this article gives the code, you can also realize that the verification code oh there is a need for friends, reference under it.


Copy Code code as follows:


<?php


/**


* Imitation QQ Verification Code


*/


//session Save Path


$sessSavePath = dirname (__file__). " /.. /data/sessions/";


if (is_writeable ($sessSavePath) && is_readable ($sessSavePath)) {Session_save_path ($sessSavePath);}


session_start ();


//Get random character


$rndstring = ';


for ($i =0 $i <4; $i + +) $rndstring. = Chr (Mt_rand (65,90));


$img _height=45; First define the long, wide
of the picture

$img _width=10;


//If GD is supported, the drawing


if (function_exists ("Imagecreate"))


{


//firefox Part of the situation will be requested multiple times, refresh the page in 5 seconds will not change session


$ntime = time ();


if (Empty ($_session[' dd_ckstr_last ')) | | | empty ($_session[' dd_ckstr ']) | | ($ntime-$_session[' Dd_ckstr_last '] > 5))


{


$_session[' dd_ckstr ' = Strtolower ($rndstring);


$_session[' dd_ckstr_last ' = $ntime;


}


$rndstring = $_session[' dd_ckstr '];


$rndcodelen = strlen ($rndstring);


//Create picture and set background color


$im = imagecreate (46,20);


imagecolorallocate ($im, 240,243,248);


//Interference line


$lineColor 1 = imagecolorallocate ($im, Mt_rand (174,218), Mt_rand (190,225), Mt_rand (217,237));


for ($j =1 $j <=2; $j = $j +3)


{


imageline ($im, 0, $j +mt_rand (1,15), $j +mt_rand (1,15), $lineColor 1);


}


//Output text


$fontColor = imagecolorallocate ($im, Mt_rand (0,150), Mt_rand (0,150), Mt_rand (0,150));


for ($i =0; $i < $rndcodelen; $i + +)


{


$BC = Mt_rand (0,1);


$rndstring [$i] = Strtoupper ($rndstring [$i]);


imagestring ($im, Mt_rand (3,5), $i * $img _height/4+mt_rand (1,5), Mt_rand (1, $img _WIDTH/2), $rndstring [$i], $ FontColor);


}


header ("Pragma:no-cache" R "N");


header ("Cache-control:no-cache" R "N");


header ("expires:0" R "N");


//output a specific type of picture format, priority for GIF-> jpg->png


if (function_exists ("Imagepng"))


{


header ("Content-type:image/png" R "N");


imagepng ($im);


}


Else


{


header ("Content-type:image/jpeg" R "N");


imagejpeg ($im);


}


Imagedestroy ($im);


exit ();


}


Else


{


//Does not support GD, output only the letter ABCD


$_session[' dd_ckstr ' = "ABCD";


$_session[' dd_ckstr_last '] = ';


Header ("Content-type:image/png" R "N");


header ("Pragma:no-cache" R "N");


header ("Cache-control:no-cache" R "N");


header ("expires:0" R "N");


$fp = fopen ("Data/vdcode.jpg", "R");


Echo fread ($fp, FileSize ("data/vdcode.jpg"));


fclose ($FP);


exit ();


}


?>


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.