-7

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

Snoobih
  • 311
  • 2
  • 9

3 Answers3

2

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;
    }
ashish.negi
  • 502
  • 3
  • 7
1

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);
SubjectCurio
  • 4,702
  • 3
  • 32
  • 46
  • thank you its helping me alot to understand and yes its useful :) – Snoobih Jul 31 '14 at 10:15
  • @Snoobih it's fine. As mentioned in my comment above, the aim is for you to try your own code and people here will help you make it work, rather than just giving you an answer. This way if you need to change it in the future, you'll know how it works because you'll have written it! and you won't need to spend more of your time asking again. But if people are just going to give you answers anyway, you might as well have an answer you can understand how it works rather than being told to "just use this" etc... – SubjectCurio Jul 31 '14 at 10:19
  • i did i just didnt post it u can chk it i modify my topic – Snoobih Jul 31 '14 at 10:24
0

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;
    }
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51