知識點:
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>