I just felt like coming up with something more OOP, and threw this together. Not claiming it's great, just something different. 🙂
<?php
header('Content-Type: text/plain');
$pwd = new PasswordGenerator(10,12);
echo $pwd->password();
class PasswordGenerator
{
private $min;
private $max;
// edit this for the symbols you want to allow
private $symbols = array('.', ',', '!', '?', '$', '-', '_', '+', '@', '&', '*');
private $password;
public function __construct($min=8, $max=10)
{
if($max < $min or $min < 4) {
throw new Exception("Invalid min/max password length");
}
$this->min = $min;
$this->max = $max;
$this->generate();
}
public function generate()
{
$password = array();
// make sure we get at least one of each type
$types = array('int', 'lower', 'upper', 'symbol');
foreach($types as $type) {
$password[] = $this->$type();
}
// emphasize letters a bit more for the rest
$types[] = 'lower';
$types[] = 'upper';
$length = rand($this->min, $this->max);
while(count($password) < $length) {
$type = $types[array_rand($types)];
$password[] = $this->$type();
}
shuffle($password);
$this->password = implode('', $password);
return $this->password;
}
public function password()
{
return $this->password;
}
private function int()
{
return rand(0, 9);
}
private function lower()
{
return chr(rand(97, 122));
}
private function upper()
{
return chr(rand(65, 90));
}
private function symbol()
{
return $this->symbols[array_rand($this->symbols)];
}
}