php生成隨機驗證碼

一、建立一個code.php文件,來生成隨機驗證碼圖片
1
<?php 2 /** 3 * Created by PhpStorm. 4 * User: ping 5 * Date: 2018/9/19 6 * Time: 9:44 7 */ 8 session_start();//開啓session 9 //定義要建立圖片的類型爲png 10 header ('Content-Type: image/png'); 11 //建立一個真彩色圖像 12 $image=imagecreatetruecolor(100, 30); 13 //定義圖片的顏色 14 $color=imagecolorallocate($image, 255, 255, 255); 15 //填充畫布顏色 16 imagefill($image, 20, 20, $color); 17 $code=''; 18 //生成隨機4個數 19 for($i=0;$i<4;$i++){ 20 $fontSize=8; 21 $x=rand(5,10)+$i*100/4;//生成橫座標位置,防止橫向不重疊 22 $y=rand(5, 15); 23 $data='abcdefghijklmnopqrstuvwxyz123456789';//定義字符串 24 $string=substr($data,rand(0, strlen($data)),1);//使用substr隨機截取一個字符 25 $code.=$string;//將截取出來的字符拼接成字符串 26 $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));//產生一個隨機色 27 imagestring($image, $fontSize, $x, $y, $string, $color);//將字符放到畫布上 28 } 29 30 $_SESSION['code']=$code;//將隨機產生的字符串存儲在session裏 31 setcookie(session_name(),session_id(),time()+3600,"/");//設置session的過時時間和路徑 32 //生成200個點 33 for($i=0;$i<200;$i++){ 34 $pointColor=imagecolorallocate($image, rand(100, 255), rand(100, 255), rand(100, 255));//生成一個隨機色,做爲點的顏色 35 imagesetpixel($image, rand(0, 100), rand(0, 30), $pointColor);//將點放到畫布 36 } 37 //生成橫線 38 for($i=0;$i<2;$i++){ 39 $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));//生成隨機色,做爲橫線的顏色 40 imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint); 41 } 42 imagepng($image); //在瀏覽器上顯示圖片 43 imagedestroy($image);//銷燬圖片 44 ?>

二、login.php登陸界面引入生成的隨機驗證碼圖片php

 1 <?php
 2 /**
 3  * Created by PhpStorm.
 4  * User: ping
 5  * Date: 2018/9/17
 6  * Time: 14:10
 7  */
 8 include "header.php";
 9 ?>
10 <!doctype html>
11 <html lang="en">
12 <head>
13     <meta charset="UTF-8">
14     <meta name="viewport"
15           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
16     <meta http-equiv="X-UA-Compatible" content="ie=edge">
17     <title>Document</title>
18     <script src="../public/lib/jQuery/jquery-3.3.1.js"></script>
19 </head>
20 <body>
21 <!--action="../controller/handel.php"將登陸界面傳送到後臺handel.php驗證-->
22 <form action="../controller/handel.php" method="post">
23 <!--    傳入一個隱藏的輸入框,用於提交到後臺,驗證是哪一個頁面傳來的值-->
24     <input type="hidden" value="login" name="methods">
25     <lable>用戶名:</lable><input type="text" name="username"><br>
26     <lable>密 碼:</lable><input type="password" name="pwd"><br>
27     驗證嗎:<input type="text" name="code" >
28 <!--            以插入圖片的形式引入code.php-->
29     <img src="code.php" alt="" id="inputcode">
30     <span>點擊圖片刷新</span><br>
31     <input type="submit" value="登陸">
32 </form>
33 <script>
34     //點擊圖片從新加載一個驗證碼圖片
35     $("#inputcode").click(function () {
36        $(this).attr("src","captcha.php");
37     })
38 </script>
39 </body>
40 
41 </html>

3.在Handel.php裏面接收登陸界面傳來的值進行驗證html

<?php
if ($route == "login"){
    $code = $_POST["code"];//接收login.php傳過來的用戶輸入的代碼
    if ($code == $_SESSION['code']){//判斷session裏存的code和用戶輸入的code是否一致
        echo "<script>window.location='../view/login.php';alert('驗證碼驗證成功,請從新輸入!!')</script>";
    }else{
        echo "<script>window.location='../view/login.php';alert('驗證碼不正確,請從新輸入!!')</script>";
    }

}
?>
相關文章
相關標籤/搜索