本文實例講述了php/JS實現的生成隨機密碼(驗證碼)功能。分享給大家供大家參考,具體如下:
PHP寫法:
//A-Z a-z 0-9 !#$%^&*$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%^&*";//生成隨機字符function createPwd($str,$len){ $pwd = ''; $strlen = strlen($str); for($i=0;$i<$len;$i++){ $pwd .= $str{mt_rand(0,$strlen-1)}; } return $pwd;}echo createPwd($str,16);
運行結果:
TX!kXjmpZeHS7GOO
js寫法
//A-Z a-z 0-9 !#$%^&*var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%^&*";//生成隨機字符function createPwd(str,len){ var pwd = ''; var maxPos = str.length; for(var i=0;i<len;i++){ pwd += str.charAt(Math.floor(Math.random()*maxPos)); } return pwd;}var passwd = createPwd(str,16);document.write(passwd);
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選