php實現簡單的驗證碼功能

來源:互聯網
上載者:User

標籤:工作   action   view   method   normal   bst   點擊   div   pac   

1.根據php中的GD庫對圖片進行處理,繪製出驗證碼的圖片,code.php中
2.表單介面,簡單的session儲存及與使用者輸入對比,確定是否驗證正確,form.php中

<?php session_start();//必須位於指令碼的最頂端 $image=imagecreatetruecolor(100, 30);//imagecreatetruecolor函數建一個真彩色映像 //產生彩色像素 $bgcolor=imagecolorallocate($image, 255, 255, 255);//白色背景 imagecolorallocate函數為一幅映像分配顏色 $textcolor=imagecolorallocate($image,0,0,255);//藍色文本 //填充函數,xy確定座標,color顏色執列區域填充顏色 imagefill($image, 0, 0, $bgcolor); $captch_code="";//初始空值 //該迴圈,迴圈取數 for($i=0;$i<4;$i++){ $fontsize=6; $x=($i*25)+rand(5,10); $y=rand(5,10);//位置隨機 // $fontcontent=$i>2?chr(rand(97,122)):chr(rand(65,90));//是小寫,否則是大寫 $data=‘abcdefghijkmnpqrstuvwxyz3456789‘; $fontcontent=substr($data,rand(0,strlen($data)-1),1);//strlen僅僅是一個計數器的工作 含數字和字母的驗證碼 //可以理解為數組長度0到30
$fontcolor=imagecolorallocate($image,rand(0,100),rand(0,100),rand(0,100));//隨機的rgb()值可以自己定
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); //水平地畫一行字串    $captch_code.=$fontcontent;}    $_SESSION[‘authcode‘]=$captch_code;//將變數儲存再session的authcode變數中        //該迴圈,迴圈畫背景幹擾的點    for($m=0;$m<=600;$m++){     $x2=rand(1,99);     $y2=rand(1,99);     $pointcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));    imagesetpixel($image,$x2,$y2,$pointcolor);// 水平地畫一串像素點    }    //該迴圈,迴圈畫幹擾直線   for ($i=0;$i<=10;$i++){       $x1=rand(0,99);       $y1=rand(0,99);       $x2=rand(0,99);       $y2=rand(0,99);       $linecolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));       imageline($image,$x1,$y1,$x2,$y2,$linecolor);//畫一條線段      }   header(‘content-type:image/png‘);   imagepng($image);   //銷毀   imagedestroy($image);?>


<--------------------------------code.php---------------------------結束----------------------------------------->
<?php// session 存值並匹配使用者輸入值    if (isset($_REQUEST[‘authcode‘])) {        session_start();        if (strtolower($_REQUEST[‘authcode‘])==$_SESSION[‘authcode‘]) {//strtolower轉化為小寫函數            echo"輸入正確!";            # code...        }        else{            echo"輸入錯誤!";        }        exit();    }?><!DOCTYPE html><html lang="en"><head><!-- 簡單的表單提交代碼 -->    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>簡單驗證碼的實現</title></head><body>    <form method="post" action="./form.php">    <p>驗證碼圖片:<img src="code.php" onClick="this.src=‘code.php?nocache=‘+Math.random()" style="cursor:hand" alt="點擊換一張"/>點擊圖片可更換驗證碼</p>    <p>請輸入圖片中的內容:<input type="text" name="authcode" value=""/></p>    <p><input type="submit" width="20px" height=19px value="提交"></input></p>    </form>    </body></html>

<---------------------------------form.php 表單介面---------------結束---------------------------------------------->

 

php實現簡單的驗證碼功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.