WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags.
Categories : PHP, PHP Classes, Algorithms, Security
Víctor Román Archidona
Date : Jul 09th 2007
Grade : 3 of 5 (graded 1 times)
Viewed : 4828
File : 4671.zip
Images : No Images for this code example.
Search : More code by Víctor Román Archidona
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

This code exmaple also shows how to get values from an array using ranges.


Usage Example
<?php
require_once "password.class.php";

$password = new PasswordCreator();
echo
$password->getPassword(64, PC_MINUS)."\n";
echo
$password->getPassword(64, PC_MINUS | PC_CAPITAL)."\n";
echo
$password->getPassword(64, PC_MINUS | PC_CAPITAL | PC_NUMBERS)."\n";
echo
$password->getPassword(64, PC_MINUS | PC_CAPITAL | PC_NUMBERS | PC_SPACES)."\n";
echo
$password->getPassword(64, PC_MINUS | PC_CAPITAL | PC_NUMBERS | PC_SPACES | PC_EXTENDED)."\n";

echo
"\n";
?>



password.class.php
<?php
/*
* Simple password generator
*
* Copyright (C) 2007 Victor Roman Archidona
* http://blog.daijo.org
*/
define ('PC_MINUS',    0x01);
define ('PC_CAPITAL'0x02);
define ('PC_NUMBERS'0x04);
define ('PC_SPACES',   0x08);
define ('PC_EXTENDED', 0x10);

class
PasswordCreator
{
   
/**
     * Contains the ASCII table representation
     *
     * @access private
     * @var array
     */
   
protected $_ascii_table = null;

    public function
__construct()
    {
       
$this->populateAsciiTable();
    }

   
/**
     * Gets a random password with the specified length and flags
     *
     * @param int $length
     * @param int $flags
     * @return string
     */
   
public function getPassword($length = 16, $flags = PC_MINUS)
    {
       
/* If there is no length we throw an exception: */
       
if ($length < 1)
            throw new
Exception ("Password lenght must be at least 1 character lenght");

       
/* Get the character matrix and its lenght: */
       
$characters = $this->getPasswordCharacters($flags);
       
$characters_length = count($characters);

       
/* Build the password: */
       
for ($i = 0; $i < $length; $i++) {
           
$random = rand(0, $characters_length - 1);
           
$output .= $characters[$random];
        }

       
/* Return it: */
       
return $output;
    }

   
/**
     * Gets the final character matrix with the specified flags.
     *
     * @param int $flags
     * @return array
     */
   
protected function getPasswordCharacters($flags)
    {
       
$chars_to_use  = array();

        if (
$flags & PC_MINUS) {
           
$chars_minus   = $this->getAsciiRange('a', 'z');
           
$chars_to_use = array_merge($chars_to_use, $chars_minus);
        }

        if (
$flags & PC_CAPITAL) {
           
$chars_mayus   = $this->getAsciiRange('A', 'Z');
           
$chars_to_use = array_merge($chars_to_use, $chars_mayus);
        }

        if (
$flags & PC_NUMBERS) {
           
$chars_numbers = $this->getAsciiRange(48,   57);
           
$chars_to_use = array_merge($chars_to_use, $chars_numbers);
        }

        if (
$flags & PC_SPACES) {
           
$chars_spaces  = $this->getSpaces();
           
$chars_to_use = array_merge($chars_to_use, $chars_spaces);
        }

        if (
$flags & PC_EXTENDED) {
           
$chars_ext     = $this->getAsciirange(128, 255);
           
$chars_to_use  = array_merge($chars_to_use, $chars_ext);
        }

        return
$chars_to_use;
    }

   
/**
     * Gets the ASCII characters between the specified range
     *
     * @param int $start First character
     * @param int $end Last character
     * @return array
     */
   
protected function getAsciiRange($start, $end)
    {
       
$start = is_numeric($start) ? $start : ord($start);
       
$end   = is_numeric($end)   ? $end   : ord($end);

        for (
$i = $start; $i <= $end; $i++)
           
$output[] = $this->_ascii_table[$i];

        return
$output;
    }

   
/**
     * Gets the space characters (tab, newline and space)
     *
     * @return array
     */
   
protected function getSpaces()
    {
       
$spaces = array("\t", "\n", " ");

        foreach (
$spaces as $space)
           
$output[] = ord($space);

        return
$output;
    }

   
/**
     * Populates the internal ASCII table
     */
   
private function populateAsciiTable()
    {
        for (
$i = 0; $i <= 255; $i++)
           
$this->_ascii_table[] = chr($i);
    }
}
?>



A damaged image generator (class) for validating text. CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart
Categories : PHP, PHP Classes, Security, GD image library, Security
Generating and Matching Secure and Strong Password Hash
Categories : PHP, PHP Classes, Cryptography, Security
Dollar Serial Number Validator
Categories : PHP, Security, Algorithms
Use of bitmasks to represent permissions
Categories : PHP, Authentication, Bitwise Operators, Security, PHP Classes
Object() = Custom __autoload + Singleton. "automagically" instantiates a class and always retuns the same instance of the same class. It's pretty useful when you want to have persistence in objects.
Categories : PHP, PHP Classes, Algorithms
SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge response login algorithms over an insecure connection.
Categories : Algorithms, PHP, Security
Scan Apache access log files and report possible worms attack
Categories : PHP, PHP Classes, Security, Apache, Log Files
Scramble Eggs - php class to scramble/encode
Categories : PHP, PHP Classes, Security, Encryption
A class to put get and post variables in hidden form elements. Works on scalars, normal arrays, associative arrays.
Categories : Algorithms, Variables, Arrays, PHP, PHP Classes
PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character.
Categories : PHP, Algorithms, Security, Authentication, Encryption
Antispoof - a class to help prevent people hi-jacking and misusing parts of a website
Categories : PHP, PHP Classes, Security
Timer - a class that uses microtime() to provide easy calculation of elapsed times
Categories : Algorithms, PHP, PHP Classes
IPhider Obscure Any URL Anonymity connection lores obfuscation corporate survival.
Categories : PHP, Algorithms, Security, URLs
Forms protected from XSS attacks (FOPAXSS)
Categories : PHP, PHP Classes, Form Processing, Security
Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers.
Categories : PHP, PHP Classes, Credit Cards, Ecommerce, Algorithms