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
生成随机密码类
随机密码生成器、随机密码生成、随机密码生成器 在线、随机账号密码生成器、linux生成随机密码,以便于您获取更多的相关知识。

时间: 2024-08-01 21:52:19

PHP生成随机密码类分享_php实例的相关文章

个人写的PHP验证码生成类分享_php实例

此验证码类直接拿去就可以用,也可以参考! 其中类成员codestr是生成的验证码字符串: <?php /** * 验证码 */ class Code{ // 1. 定义各个成员 有宽.高.画布.字数.类型.画类型 private $width; //宽度 private $height; //高度 private $num; //验证码字数 private $imgType; //生成图片类型 private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小

一个经典的PHP验证码类分享_php实例

我们通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中.只要在创建对象时,为构造方法提供三个参数,包括创建验证码图片的宽度.高度及验证码字母个数,就可以成功创建一个验证码类的对象.该类的声明代码如下所示: <?php class Vcode { private $width; //宽 private $height; //高 private $num; //数量 private $code

支持png透明图片的php生成缩略图类分享_php技巧

注:此功能依赖GD2图形库 最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图 试用了一下后,发现有这样几个问题: 1.png图片生成的缩略图是jpg格式的 2.png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景) 3.代码语法比较老 因此,在这个版本的基础上简单修改优化了一下. PHP生成缩略图类 <?php /* * desc: Resize Image(png, jpg, gif) * author: 十年后的卢哥哥 * date: 2014.11.

PHP生成随机密码方法汇总_php实例

使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码.随机密码也就是一串固定长度的字符串,这里我收集整理了几种生成随机字符串的方法,以供大家参考. 方法一:      1.在 33 – 126 中生成一个随机整数,如 35,     2.将 35 转换成对应的ASCII码字符,如 35 对应 #     3.重复以上 1.2 步骤 n 次,连接成 n 位的密码      该算法主要用到了两个函数,mt_rand ( int $

php生成验证码,缩略图及水印图的类分享_php实例

封装了一个类,可生成验证码,缩略图,及水印图,分享给大家 <?php class image{ session_start(); //验证码类 static public function verify($code,$width=75,$height=25,$n=4){ header("content-type:image/png"); // 创建画布 $img=imagecreatetruecolor($width,$height); // 设置背景色 $bgcolor=ima

一个比较不错的PHP日历类分享_php实例

说到对时期和时间的处理,就一定要介绍一下日历程序的编写.但一提起编写日历,大多数人都会认为日历的作用只是为了在页上显示当前的日期,其实日历在我们的开发中有更重要的作用.例如我们开发一个"记事本"就需要通过日历设定日期,还有一些系统中需要按日期去排任务,也需要日历,等等.本例涉及的日期和时间函数并不是很多,都是前面介绍的内容,主要是通过一个日历类的编写,巩固一下前面介绍过的面向对象的语法知识,以及时间函数应用,最主要的是可以提升初学者的思维逻辑和程序设计能力.将日历类Calendar声明

一个经典的PHP文件上传类分享_php实例

文件上传是项目开发中比较常见的功能,但文件上传的过程比较繁琐,只要是有文件上传的地方就需要编写这些复杂的代码.为了能在每次开发中降低功能的编写难度,也为了能节省开发时间,通常我们都会将这些反复使用的一段代码封装到一个类中.帮助开发者在以后的开发中,通过编写几条简单代码就可以实现复杂的文件上传功能.对于基础薄弱的读者,只要会使用本类即可,而对一些喜欢挑战的朋友,可以尝试去读懂它,并能开发一个属于自己的文件上传类. 一.需求分析 要球自定义文件上传类,即在使用非常简便的前提下,又可以完成以下几项功能

使用gd库实现php服务端图片裁剪和生成缩略图功能分享_php实例

裁剪示例: 最终裁剪成的图片: 其中虚线框内就是要裁剪出来的图片,最终保存成100宽的图片.代码如下: 复制代码 代码如下: $src_path = '1.jpg';//创建源图的实例$src = imagecreatefromstring(file_get_contents($src_path)); //裁剪开区域左上角的点的坐标$x = 100;$y = 12;//裁剪区域的宽和高$width = 200;$height = 200;//最终保存成图片的宽和高,和源要等比例,否则会变形$fi

PHP 验证登陆类分享_php实例

简单的登录类,没有把登录和数据库查询分开 复制代码 代码如下: /*  *   例子  *  *  $Auth=new Auth();  *  $Auth->login("123@123.com","123");  *  $Auth->logout();  *  echo $r->init();      * **/ 验证登陆类 复制代码 代码如下: <?php /*  *  * @ID:      验证登陆类  *  * @class: