利用PHP產生隨機驗證碼

大家都知道一般網站的登陸除了賬號密碼之外,還需輸入驗證碼進行驗證。今天,給大家分享一個利用PHP產生隨機驗證碼的案例。


這個案例是一個用面向對象的PHP寫的,下面給出其源碼:


<?php 
class RandString{
    private $length;
    private $type;
    
    public function __construct($length,$type){
        $this->length=$length;
        $this->type=$type;
    }
    public function randNum()
    {
        switch ($this->type){
            case 1:
                return join(array_rand(range(0, 9),$this->length));
                break;
            case 2:
                return join(array_rand(array_flip(array_merge(range('0', '9'),range('a', 'z'),range('A','Z'))),$this->length));
                break;


        }
    }
}
$strNum=new RandString(4,2);
echo $strNum->randNum();
?>

利用php數組存儲隨機數函數從0·9,a-z中產生的4個字符,然後只需將這4個隨機字符進行表單驗證,再進行包裝即可成爲大家所看到的登陸動態驗證碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章