My first PHP Custom function: Verification code generation

Source: Internet
Author: User
Tags border color


/* *

*_nmsg () Generate Verification code
* @access Public
* @param int $_width Verification code width
* @param int$_height Verification Code height
* @param int$_rnd_count Verification Code bit number
* @param bool $_rnd_count Verification code border
*return void
*/

function_nmsg ($_width=75,$_height= 25,$_rnd_count=4,$_flag=true){//Generate random numbers    Session_Start();  for($i= 0;$i<$_rnd_count;$i++){        @$_nmsg.=Dechex(Mt_rand(0,15)); }    $_session[' Nmsg ']=$_nmsg; //Create an image    Header("Content-type:image/png"); $_img=imagecreatetruecolor ($_width,$_height); //background color (white)    $_white=imagecolorallocate ($_img, 255,255,255); //Fill ColorImagefill ($_img, 0, 0,$_white); //border Color (black)    $_black=imagecolorallocate ($_img, 0,0,0); //Border Switch    if($_flag){    //Create a borderImagerectangle ($_img, 0, 0,$_width-1,$_height-1,$_black); }    //Random Line Generation     for($i= 0;$i<6;$i++){        $_rnd_color=imagecolorallocate ($_img,Mt_rand(200,255),Mt_rand(200,255),Mt_rand(200,255)); Imageline ($_img,Mt_rand(2,$_width-2),Mt_rand(2,$_height-2),Mt_rand(2,$_width-2),Mt_rand(2,$_height-2),$_rnd_color); }    //Random Snowflakes     for($i= 0;$i<100;$i++){        $_rnd_color=imagecolorallocate ($_img,Mt_rand(200,255),Mt_rand(200,255),Mt_rand(200,255)); Imagestring ($_img, 1,Mt_rand(1,$_width-7),Mt_rand(1,$_height-7), ' * ',$_rnd_color); }    //Output Verification Code     for($i= 0;$i<$_rnd_count;$i++){        $_rnd_color=imagecolorallocate ($_img,Mt_rand(0,150),Mt_rand(0,150),Mt_rand(0,150)); Imagestring ($_img,Mt_rand(3,5),$i*$_width/$_rnd_count+Mt_rand(1,10),Mt_rand(1,$_height/2),$_session[' Nmsg '] [$i],$_rnd_color); }    //Output ImageImagepng ($_img); //Destroying ImagesImagedestroy ($_img);}

JS Refresh Section

window.onload=function() {    var nmsg = document.getElementById (' nmsg ');    Nmsg.onclick=function() {        nmsg.src= ' code.php?tm= ' +math.random ();    };}

My first PHP Custom function: Verification code generation

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.