Home > Backend Development > PHP Tutorial > PHP+javascript creates verification code with prompts source code sharing_PHP tutorial

PHP+javascript creates verification code with prompts source code sharing_PHP tutorial

WBOY
Release: 2016-07-13 10:29:14
Original
1088 people have browsed it

html代码:

复制代码 代码如下:





简单验证码





   
     
     
     
     
     
     
验证码:

       
<script>yzm()</script>看不清
请输入验证码

 



JS代码:

复制代码 代码如下:

function chkyzm(form){     //对验证码进行验证
 if(form.yzm.value==""){
  yzm1.innerHTML="×验证码不能为空";
 }else if(form.yzm.value!=form.yzm2.value){
  yzm1.innerHTML="×验证码输入错误";
 }else{
  yzm1.innerHTML="√验证码输入正确";
 }
}
function yzm(){      //生成验证码
 var num1=Math.round(Math.random()*1000000);//随机小数放大
 var num=num1.toString().substr(0,4);//取4位整数
 var yzm2=document.getElementById("yzm2");
 document.write("");
 yzm2.value=num;
}
function code(){      //重置验证码
 var num1=Math.round(Math.random()*1000000);
 var num=num1.toString().substr(0,4);
 var yzm2=document.getElementById("yzm2");
 document.codeimg.src="yzm.php?num="+num;
 yzm2.value=num;
}

yzm.php代码:

复制代码 代码如下:

header("Content-type: image/png");
$im=imagecreate(66,22); //Create canvas
$black=imagecolorallocate ($im,0,0,0); //Define background
$white=imagecolorallocate($im,255,255,255); //Define background
$gray=imagecolorallocate($im,200,200,200); //Definition Background
imagefill($im,0,0,$gray); //Fill color
for($i=0;$i<4;$i++){ //Define 4-digit random number
$str=mt_rand(1,5); //Define the Y coordinate of the random character location
$size=mt_rand(6,9); //Define the font of the random character
$authnum=substr($ _GET[num],$i,1); //Get the verification code passed in the hyperlink
imagestring($im,$size,(3+$i*15),$str,$authnum,imagecolorallocate($ IM, RAND (0,250), RAND (0,250), RAND (0,250)); // RAND (0,500) the blur of the number
} // horizont i<200;$i++){ //Execute the for loop and add a blurred background to the verification code
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); //Create Background
imagesetpixel($im,rand()%70,rand()%30,$randcolor); //Draw a single element
}
imagepng($im); //Generate png image
imagedestroy($im); //Destroy the image
?>

Note: PHP needs to be configured to execute related methods.

Operating effect:

http://www.bkjia.com/PHPjc/777638.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/777638.htmlTechArticlehtml code: Copy the code as follows: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ /EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template