大家都知道一般網站的登陸除了賬號密碼之外,還需輸入驗證碼進行驗證。今天,給大家分享一個利用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個隨機字符進行表單驗證,再進行包裝即可成爲大家所看到的登陸動態驗證碼。