PHP生成随机密码类分享
2015-01-24信息快讯网
这篇文章主要介绍了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采用curl实现伪造IP来源的方法
php文件缓存类汇总
php实现事件监听与触发的方法
PHP通过内置函数memory_get_usage()获取内存使用情况
IIS下PHP的三种配置方式对比
PHP GD库生成图像的几个函数总结
PHP生成不重复随机数的方法汇总
使用PHP把HTML生成PDF文件的几个开源项目介绍
php生成随机颜色的方法
PHP动态页生成静态页的3种常用方法
php中实现记住密码下次自动登录的例子
php密码生成类实例
php生成随机数的三种方法
PHP生成图片验证码、点击切换实例
PHP网页游戏学习之Xnova(ogame)源码解读(十二)
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
PHP获取windows登录用户名的方法
PHP获取MySql新增记录ID值的3种方法
PHP判断表单复选框选中状态完整例子
destoon找回管理员密码的方法
PHP基于GD库的缩略图生成代码(支持jpg,gif,png格式)
采用thinkphp自带方法生成静态html文件详解
CI使用Tank Auth转移数据库导致密码用户错误的解决办法
Codeigniter生成Excel文档的简单方法
PHP图片等比例缩放生成缩略图函数分享
浅谈discuz密码加密的方式
php+highchats生成动态统计图
php生成随机密码自定义函数代码(简单快速)
phpmyadmin配置文件现在需要绝密的短密码(blowfish_secret)的2种解决方法
在PHP模板引擎smarty生成随机数的方法和math函数详解
php生成随机字符串可指定纯数字、纯字母或者混合的
php使用codebase生成随机数
PHP随机生成随机个数的字母组合示例