Hi I started to learn a little php today and managed to convert one of my C# codes to php this example allows you to make random password.
Hope you like it,comments and suggestions welcome.
PHP Code:
<?php
/**
* @author Ben-Jones
* @version 1.0
* @name Rnadom Password Generator.
* Script to generate random password by a given length.
*/
hi just thought I show you an update. Today I managed to convert my random password generator to a class.
I also added some more little features for creating passwords. Hope you like the new update. Comments and suggestions welcome.
also see Readme.txt file for info on installing and using the script.
<?php
# Name: passwords.php
# File Description: generates a list of "random" passwords that meet the requirements filled out in teh form
# Author: ricocheting
# Web: http://www.ricocheting.com/
# Update: 2011-04-29
# Version: 1.0.0
# Copyright 2011 ricocheting.com
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$length=8;//default password length
$count=10;//default number of passwords
$chk_upper=!empty($_GET['chk_upper']);
$chk_lower=!empty($_GET['chk_lower']);
$chk_digit=!empty($_GET['chk_digit']);
$chk_special=!empty($_GET['chk_special']);
// if no char sets selected: default
if(!$chk_upper && !$chk_lower && !$chk_digit && !$chk_special){
$chk_safe=true;
$chk_upper=true;
$chk_lower=true;
$chk_digit=true;
$chk_special=false;
$chk_require=true;
$chk_startdigit=true;
}
// use what was selected by user
else{
$chk_safe=!empty($_GET['chk_safe']);
$chk_require=!empty($_GET['chk_require']);
$length=(!empty($_GET['length'])?(int)$_GET['length']:$length);
if($length>20)$length=20;// capped at 20 chars max length
if($length<4)$length=4;// capped at 4 chars min length
$count=(!empty($_GET['count'])?(int)$_GET['count']:$count);
if($count>99)$count=99;// capped at 99 passwords max
if($count<1)$count=1;// capped at 1 password min
}
$str_upper='';
$str_lower='';
$str_digit='';
$str_special='';
// characters that will be used
if($chk_upper==1){
$str_upper="ABCDEFGHJKLMNPQRSTUVWXYZ";
if($chk_safe!=1) $str_upper.="IO";
}
if($chk_lower==1){
$str_lower.="abcdefghjkmnpqrstuvwxyz";
if($chk_safe!=1) $str_lower.="iol";
}
if($chk_digit==1){
$str_digit.="23456789";
if($chk_safe!=1) $str_digit.="01";
}
if($chk_special==1){
$str_special.="~!@#$%^&*()_+[]\;',./~{}|:\"<>?";
}
$use=$str_upper.$str_lower.$str_digit.$str_special;
$use_length=strlen($use)-1;
echo '<pre>';
// number of passwords to create
for($x=0; $x<$count; ++$x){
$password='';
// if requiring one of each type, preload the password with those. is randomized later with str_shuffle()
if($chk_require==1){
if($chk_upper==1) $password.=$str_upper[rand(0,(strlen($str_upper)-1))];
if($chk_lower==1) $password.=$str_lower[rand(0,(strlen($str_lower)-1))];
if($chk_digit==1) $password.=$str_digit[rand(0,(strlen($str_digit)-1))];
if($chk_special==1) $password.=$str_special[rand(0,(strlen($str_special)-1))];
}
// length to make password (minus the characters already forced by require)
for($i=strlen($password); $i<$length; ++$i){
$password.=$use[rand(0,$use_length)];
}
// shuffle the char order then print (with any special chars html safe)
echo htmlentities(str_shuffle($password))."\n";
}
echo '</pre>';
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="get">
<input type="checkbox" name="chk_safe"<?php echo ($chk_safe?' checked="checked"':''); ?>> Hide confusing characters<br />
<input type="checkbox" name="chk_upper"<?php echo ($chk_upper?' checked="checked"':''); ?>> Allow uppercase characters<br />
<input type="checkbox" name="chk_lower"<?php echo ($chk_lower?' checked="checked"':''); ?>> Allow lowercase characters<br />
<input type="checkbox" name="chk_digit"<?php echo ($chk_digit?' checked="checked"':''); ?>> Allow digits<br />
<input type="checkbox" name="chk_special"<?php echo ($chk_special?' checked="checked"':''); ?>> Allow special characters<br />
<input type="checkbox" name="chk_require"<?php echo ($chk_require?' checked="checked"':''); ?>> Require at least one character of each chosen type<br />
<input type="text" name="length" value="<?php echo $length;?>" size="2" maxlength="3"> Password length<br />
<input type="text" name="count" value="<?php echo $count;?>" size="2" maxlength="3"> How many passwords<br />
<input type="submit" value="Create">
</form>
Last edited by bestellen; Sep 15th, 2015 at 01:38 PM.