PHP Classes

File: PasswordGenerator.php

Recommend this page to a friend!
  Classes of Kevin Gilbertson   PasswordGenerator   PasswordGenerator.php   Download  
File: PasswordGenerator.php
Role: Class source
Content type: text/plain
Description: Class to generate random passwords.
Class: PasswordGenerator
Simple class to generate random passwords.
Author: By
Last change: Fixed bugs.
Date: 21 years ago
Size: 2,754 bytes
 

Contents

Class file image Download
<?php
/***
* This class is designed to create somewhat random passwords
* to set up initial user accounts where
* you email out the initial login information.
*
* Created By: Kevin Gilbertson (kevin@gilbertsonconsulting.com)
* Last Updated: 4/22/02
* Modified By: Flinn Mueller (flinn AT activeintra DOT net)
* Last Updated: 5/01/02
* Modified By: Kevin Gilbertson (kevin@gilbertsonconsulting.com)
* Last Updated: 5/15/02
* Modified By: Kevin Gilbertson (kevin@gilbertsonconsulting.com)
* Last Updated: 11/14/02
***/
class PasswordGenerator
{
    var
$passwdchars;
    var
$passwd;
    var
$length;
    var
$minlength;
    var
$maxlength;

    function
PasswordGenerator($min=6, $max=8, $special=NULL, $chararray=NULL)
    {
        if(
$chararray == NULL) {
                
$passwdstr = "abcdefghijklmnopqrstuvwxyz";
                
$passwdstr .= strtoupper($passwdstr);
                
$passwdstr .= "12345678901234567890"; // twice to up the likelyhood
            // add special chars to start
                    
if ($special) {
           
$passwdstr .= "!@#$%";
                     }
        } else {
           
$passwdstr = $chararray;
        }

            for(
$i=0; $i<=strlen($passwdstr) - 1; $i++) {
               
$this->passwdchars[$i]=$passwdstr[$i];
            }
            
           
// randomize the chars
           
srand ((float)microtime()*1000000);
           
shuffle($this->passwdchars);

       
$this->minlength = $min;
       
$this->maxlength = $max;
    }

    function
setLength() // private method
   
{ $this->length = rand($this->minlength, $this->maxlength); }

    function
setMin($min)
    {
$this->minlength = $min; }

    function
setMax($max)
    {
$this->maxlength = $max; }

    function
getPassword()
    {
       
$this->passwd = NULL;
       
$this->setLength();

        for(
$i=0; $i<$this->length; $i++)
        {
           
$charnum = rand(0, count($this->passwdchars) - 1);
           
$this->passwd .= $this->passwdchars[$charnum];
        }

        return
$this->passwd;
    }

   
// to show in browser
   
function getHTMLPassword()
    {
        return (
htmlentities($this->getPassword()));
    }

   
// Allows password to be shown as an image
    // Also semi-tempest resistant, with random text position,
    // and nifty gray color which should difuse tempest emissions
    // Created By: Flinn Mueller (flinn AT activeintra DOT net)
   
function getImgPassword()
    {
       
$RandPassword = $this->getPassword();

       
// create the image
       
$png = ImageCreate(200,80);
       
$bg = ImageColorAllocate($png,192,192,192);
       
$tx = ImageColorAllocate($png,128,128,128);
       
ImageFilledRectangle($png,0,0,200,80,$bg);
       
srand ((float)microtime()*1000000);
       
ImageString($png,5,rand(0,90),rand(0,50),$RandPassword,$tx);

       
// send the image
       
header("content-type: image/png");
       
ImagePng($png);
       
imagedestroy ($png);
    }
}
?>