您好,登錄后才能下訂單哦!
本篇內容主要講解“PHP如何生成隨機密碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PHP如何生成隨機密碼”吧!
類代碼:
<?php /** * PHP - Password Generator Class * Version 1.0.0 * */ if (@!is_object($passGen) || !isset($passGen)) { $passGen = new Password; } class Password { /** * 大寫字母 A-Z * * @var array */ protected $uppercase_chars; /** * 小寫字母 a-z * * @var array */ protected $lowercase_chars; /** * 阿拉伯數字 0-9 * * @var array */ protected $number_chars; /** * 特殊字符 * * @var array */ protected $special_chars; /** * 其他特殊字符 * * @var array */ protected $extra_chars; /** * 最終用來生成密碼的所有字符 * * @var array */ protected $chars = array(); /** * 密碼長度 * * @var array */ public $length; /** * 是否使用大寫字母 * * @var boolean */ public $uppercase; /** * 是否使用小寫字母 * * @var boolean */ public $lowercase; /** * 是否使用阿拉伯數字 * * @var boolean */ public $number; /** * 是否使用特殊字符 * * @var boolean */ public $special; /** * 是否使用額外的特殊字符 * * @var boolean */ public $extra; /** * 初始化密碼設置 * * @param int $length */ function Password($length = 12) { $this->length = $length; $this->configure(true, true, true, false, false); } /** * 配置 */ function configure($uppercase = false, $lowercase = false, $number = false, $special = false, $extra = false ) { $this->chars = array(); $this->upper_chars = array( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ); $this->lower_chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ); $this->number_chars = array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" ); $this->special_chars = array( "!", "@", "#", "$", "%", "^", "&", "*", "(", ")" ); $this->extra_chars = array( "[", "]", "{", "}", "-", "_", "+", "=", "<", ">", "?", "/", "`", "~", "|", ",", ".", ";", ":" ); if (($this->uppercase = $uppercase) === true) { $this->chars = array_merge($this->chars, $this->upper_chars); } if (($this->lowercase = $lowercase) === true) { $this->chars = array_merge($this->chars, $this->lower_chars); } if (($this->number = $number) === true) { $this->chars = array_merge($this->chars, $this->number_chars); } if (($this->special = $special) === true) { $this->chars = array_merge($this->chars, $this->special_chars); } if (($this->extra = $extra) === true) { $this->chars = array_merge($this->chars, $this->extra_chars); } $this->chars = array_unique($this->chars); } /** * 從字符列中生成隨機密碼 * * @return string **/ function generate() { if (empty($this->chars)) { return false; } $hash = ''; $totalChars = count($this->chars) - 1; for ($i = 0; $i < $this->length; $i++) { $hash .= $this->chars[$this->random(0, $totalChars)]; } return $hash; } /** * 生成隨機數字 * * @return int */ function random($min = 0, $max = 0) { $max_random = 4294967295; $random = uniqid(microtime() . mt_rand(), true); $random = sha1(md5($random)); $value = substr($random, 0, 8); $value = abs(hexdec($value)); if ($max != 0) { $value = $min + ($max - $min + 1) * $value / ($max_random + 1); } return abs(intval($value)); } }
調用:
<?php include_once 'password.class.php'; echo $passGen->generate(); //FS4yq74e2LeE
到此,相信大家對“PHP如何生成隨機密碼”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。