can somone help me to make this work i want to generate a password
Password Length: 15
Include Lowercase Characters Include Uppercase Characters
with one number in random position
ex:
Zs8ChnduGpkeuqx
ovephmVXD8RgcPr
VimDDE3txrVjLSe
here is what i was doing before and i wasnt sure if its the right way so i asked for help to get more informations ;)
<?php
function generateRandomString($length =14, $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'){
$s = '';
$lettersLength = strlen($letters)-1;
for($i = 0 ; $i < $length ; $i++)
{
$s .= $letters[rand(0,$lettersLength)];
}
return $s;
}
function generateRandomNum($length =1, $letters = '0123456789'){
$s = '';
$lettersLength = strlen($letters)-1;
for($i = 0 ; $i < $length ; $i++)
{
$s .= $letters[rand(0,$lettersLength)];
}
return $s;
}
function shuffled() {
$str = generateRandomString();
$num = generateRandomNum();
$tot = $str.$num;
$s = str_shuffle($tot);
{
return $s;
}
}
echo shuffled();
?>
thank you for your help
Guessing you might benefit from an answer that walked you through what was going on, rather than just giving you wanted, might be useful for you in the future.
function randomString($length) { // Generates a random string of $length characters long
$letters = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ"; // Characters you don't mind having more than one of
$random_string = '';
for ($i = 0; $i < $length; $i++) { // Until $i = $length, do the following & increment $i by 1 each time
$random_string .= $letters[rand(0, (strlen($letters) - 1))]; // Add another random character to the string
}
$random_string[rand(0, ($length - 1))] = rand(0,9); // Replace one of the characters with a random number between 0 and 9
return $random_string;
}
echo randomString(15);
use this function
function get_password($length = 15) {
$str = substr(str_shuffle ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$str[rand(0, ($length - 1))] = rand(0,9);
return $str;
}
try this
$password = generate_password(15, 1, 1);
function generate_password($length=8,$use_upper=0,$use_lower=0,$use_number=1,$use_custom="")
{
$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$lower = "abcdefghijklmnopqrstuvwxyz";
$number = "0123456789";
$password="";
$seed="";
$seed_length=0;
if($use_upper){
$seed_length += 26;
$seed .= $upper;
}
if($use_lower){
$seed_length += 26;
$seed .= $lower;
}
if($use_number){
$seed_length += 10;
$seed .= $number;
}
if($use_custom){
$seed_length +=strlen($use_custom);
$seed .= $use_custom;
}
for($x=1;$x<=$length;$x++){
$password .= $seed{rand(0,$seed_length-1)};
}
return $password;
}