在php中有乙個函式rand
它的作用就是產生乙個隨機整數
兩個語法為
int rand (void)
int rand (int $min,int $max)//有限制有條件的挑選隨機數
沒有條件限制的產生隨機整數,**為
$str = "abcdefghijklmnopqrstuvwxyz0123456789";
echo rand();
如果想要指定條件,**為
$str = "abcdefghijklmnopqrstuvwxyz0123456789";
echo rand(0,10);
mt_rand可代替rand函式
作用可生成更好的隨機數
由於字串為36位,索引號從0開始,所以減1位,**為
$str = "abcdefghijklmnopqrstuvwxyz0123456789";
echo mt_rand(0,$len-1);
以上都是生成一位隨機數
下面我們分析如何生成四位函式
首先我們了解乙個函式 substr
作用是返回字串的字
string substr (string $string , int $start [,int $length] )
string 是設定好的字串 start是從0開始計算
<?php
$rest = substr ( "abcdef" , - 1 ); // 返回 "f"
$rest = substr ( "abcdef" , - 2 ); // 返回 "ef"
$rest = substr ( "abcdef" , - 3 , 1 ); // 返回 "d"
?>
length是從哪個字串開始 從0開始算起,最多包括length個字元如果是負數,將倒數開始算起,後面也可以加逗號 限制個數
<?php
$rest = substr ( "abcdef" , 0 , - 1 ); // 返回 "abcde"
$rest = substr ( "abcdef" , 2 , - 1 ); // 返回 "cde"
$rest = substr ( "abcdef" , 4 , - 4 ); // 返回 ""
$rest = substr ( "abcdef" , - 3 , - 1 ); // 返回 "de"
?>
$str = "abcdefghijklmnopqrstuvwxyz0123456789"; //建立字串
$len = strlen($str); //確定長度
$yzm = '';
for($i=0; $i<4; $i++)
echo $yzm;
然後四位數的**就會產生了。以上就是四位驗證碼的講解。 php生成驗證碼
header content type image gif 初始化 border 0 是否要邊框 1要 0不要 how 4 驗證碼位數 w how 15 寬度 h 20 高度 fontsize 5 字型大小 alpha abcdefghijkmnopqrstuvwxyz 驗證碼內容1 字母 numb...
php 生成驗證碼
驗證碼個數 num 4 驗證碼寬度 width 80 驗證碼高度 height 20 驗證碼 注意是字串 code 生成驗證碼 for i 0 i num i 驗證碼儲存到session中 session start session verifycode code 建立影象 image imagec...
php 生成驗證碼
che.php session start im imagecreatetruecolor 100,30 設定顏色 bg imagecolorallocate im,0,0,0 背景色 te imagecolorallocate im,255,255,255 字型顏色 for i 0 i 4 i 輸...