Example analysis of PHP CI frame Verification Code

Source: Internet
Author: User

PHP Code:

Copy Code code as follows:


<?php


class Captcha_code


{


var $width = ' 60 ';


var $num = ' 4 ';


var $height = ' 20 ';


var $name = ' Randcode ';


Public Function __construct ($conf = "")


{


if ($conf!= "")


{


foreach ($conf as $key => $value)


{


$this-> $key = $value;


}


}


}





function Show ()


{


Header ("Content-type:image/gif");


/*


* Initialization


*/


$border = 0; Do you want to have a border 1:0 Don't


$how = $this->num; Verify number of code digits


$w = $this->width; Picture width


$h = $this->height; Picture Height


$fontsize = 5; Font size


$alpha = "ABCDEFGHIJKMNOPQRSTUVWXYZ"; CAPTCHA Content 1: Letter


$number = "023456789"; Authenticode Content 2: Digital


$randcode = ""; Authentication Code String Initialization


Srand (Double) microtime () *1000000); Initialization of random number seed





$im = imagecreate ($w, $h); Create a validation picture





/*


* Draw Basic frame


*/


$bgcolor = imagecolorallocate ($im, 255, 255, 255); Set Background color


Imagefill ($im, 0, 0, $bgcolor); Fill background Color


if ($border)


{


$black = imagecolorallocate ($im, 0, 0, 0); Set Border color


Imagerectangle ($im, 0, 0, $w-1, $h-1, $black);//Draw Border


}





/*


* generates random characters on a per-bit basis


*/


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


{


$alpha _or_number = Mt_rand (0, 1); A letter or a digital


$str = $alpha _or_number? $alpha: $number;


$which = Mt_rand (0, strlen ($str)-1); which character to take


$code = substr ($str, $which, 1); Take character


$j = $i? 4: $j +15; Draw character Position


$color 3 = imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100)); Character immediately color


Imagechar ($im, $fontsize, $j, 3, $code, $color 3); Painting character


$randcode. = $code; Step-by-step addition of Authenticode string


}





/*


* Add Interference


*/


for ($i =0 $i <5; $i + +)//Draw background interference line


{


$color 1 = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Interference Line Color


Imagearc ($im, Mt_rand ( -5, $w), Mt_rand ( -5, $h), Mt_rand (20,300), Mt_rand (20,200), $color 1); Interference Line


}


for ($i =0; $i < $how *15 $i + +)//Draw background interference points


{


$color 2 = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Interference point Color


Imagesetpixel ($im, Mt_rand (0, $w), Mt_rand (0, $h), $color 2); Jamming Point


}





//Writes the CAPTCHA string to the session





//$this->session->set_userdata (Array ($this->name=> $randcode));





$_session[$this->name]= $randcode;


/* Drawing End * *


imagegif ($im);


Imagedestroy ($im);


/* Drawing End * *


}


}


?>


Invoke PHP Code:

Copy Code code as follows:


function Verify_image () {


$conf [' name '] = ' verify_code ';
as configuration parameter

$this->load->library (' Lib_captcha ', $conf);


$this->lib_captcha->show ();


$yzm _session = $this->session->userdata (' Verify_code ');


echo $yzm _session;


}


HTML code:

Copy Code code as follows:


<dl>


<dt> Verification Code:</dt>


<dd>


<input type= "text" name= "Verify_text" id= "Verify_text" class= "Yzma" value= "" >


<img src= "/user/verify_image" alt= "Authentication Code" id= "Verify_code" class= "yz_img"/>


<a href= "Javascript:changecode ();" class= "Change_yz" > Change a sheet </a>


</dd>


<dd class= "Tips_wrong" ><b> authentication code not correct </b></dd>


<dd class= "Tips_correct" ></dd>


</dl>


JS Code:

Copy Code code as follows:


<script type= "Text/javascript" >
function ChangeCode () {
Fs.query ("#verify_code"). src = "/user/verify_image?r=" + math.random ();
}
</script>

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.