知识点:
1. session获取其他页面的变量:
(1)先在画验证码php里开启session_start(),$_SESSION['随便起名']=验证码字符串,
(2)再在submit提交到action里的php里,开启session_start(),$str = $_SESSION['刚才随便起的名'],这样这个$str就等于验证码php里的‘验证码字符串’了,相当于用session中间过度了一下。
(3)然后与get得到的输入框里的验证码比较。
2. 两个验证码同时转大写。
3. 把数组里的验证码4个字母转成字符串,implode('',$arr)。
4. js的location重定向。
test.php
1 <?php 2 session_start(); 3 $vstring = $_SESSION["vstring"]; 4 $vcode = $_GET["vcode"]; 5 $username = $_GET["username"]; 6 //$password = $_GET["password"]; 7 // echo "$username"; 8 // echo "$password"; 9 // echo "$vcode"; 10 echo($vstring); 11 //验证码全部转大写 12 $vcode = strtoupper($vcode); 13 $vstring = strtoupper($vstring); 14 if ($vcode==$vstring) { 15 echo "验证码正确!"; 16 //js重定向 17 echo "<script>location='http://www.baidu.com'</script>"; 18 }else{ 19 echo "验证码错误!"; 20 echo "<script>location='zhuce.php'</script>"; 21 } 22 23 ?>
yanzhengma.php
1 <?php 2 //开启session 3 session_start(); 4 // ob_clean(); 5 header("content-type:image/png"); 6 $width = 110; 7 $height = 40; 8 $img = imagecreatetruecolor($width, $height); 9 //$string = "hello"; 10 //7种颜色,存入数组 11 $red = imagecolorallocate($img, 255, 0, 0); 12 $white = imagecolorallocate($img, 255, 255, 255); 13 $green = imagecolorallocate($img, 0, 255, 0); 14 $blue = imagecolorallocate($img, 0, 0, 255); 15 $aaa = imagecolorallocate($img, 255, 255, 0); 16 $bbb = imagecolorallocate($img, 0, 255, 255); 17 $ccc = imagecolorallocate($img, 255, 0, 255); 18 $colors = array($white,$red,$green,$blue,$aaa,$bbb,$ccc); 19 //颜色换成随机组成的RGB,每次循环都生成一次 20 $color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); 21 //画点 22 for ($i=0; $i < 10; $i++) { 23 $color1 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); 24 imagesetpixel($img, mt_rand(0,$width), mt_rand(0,$height), $color1); 25 } 26 //划线 27 for ($i=0; $i < 4; $i++) { 28 $color2 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); 29 imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $color2); 30 } 31 //生成4位验证码 32 $a1 = range(0, 9); 33 $a2 = range(a, z); 34 $a3 = range(A, Z); 35 $a4 = array_merge($a1,$a2,$a3); 36 //改用shuffle打断顺序,array_slice取出前4个字母数字。不然如果用mt_rand在循环中每次取一个,还要生成字符串,不好比对 37 shuffle($a4); 38 $a5 = array_slice($a4,0,4); 39 $a6 = implode('', $a5); 40 //把验证码存到session 41 $_SESSION['vstring'] = $a6; 42 $num = 4; 43 $fontsize = 20; 44 for ($i=0; $i < 4; $i++) { 45 $color3 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); 46 imagettftext($img, $fontsize, mt_rand(-30,30), $width/$num*$i+5, 30, $color3, "Fonts/msyh.ttf", $a5[$i]); 47 } 48 imagepng($img); 49 ?>
zhuce.php
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>注册</title> 6 </head> 7 <body> 8 <form action="test.php" method = "get"> 9 <table> 10 <tr> 11 <td>用户名:</td> 12 <td><input type="textarea" name="username" runat="server"><br/></td> 13 </tr> 14 <tr> 15 <td>密码:</td> 16 <td><input type="password" name="password"><br/></td> 17 </tr> 18 <tr> 19 <td>验证码:</td> 20 <td><input type="text" name = "vcode" runat="server"><br/></td> 21 </tr> 22 <tr> 23 <td></td> 24 <td align="center" valign="center"><img src="yanzhengma.php" id = "yanzhengma" ></form></td> 25 </tr> 26 <tr> 27 <td><input type="submit" value="提交" ></td> 28 </tr> 29 <tr> 30 <td><input type="reset" value="重置"></td> 31 </tr> 32 </table> 33 <?php 34 $var = ' 35 <script type="text/javascript"> 36 onload = function(){ 37 var yanzhengma = document.getElementById("yanzhengma"); 38 yanzhengma.onclick = function(){ 39 this.src = "yanzhengma.php?"+Math.random(); 40 }; 41 } 42 </script> 43 '; 44 echo $var ?> 45 </body> 46 </html>