/* *
*_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