0

I want to generate a random password based on the monthly names, the function should pick alphabet from the A to Z and place to the 5th position of the random password generated?

Problem

I'm a little bit confused about how do I pick the first letter of the $alphabet base on the January and place to the 5th position of the random password generated?

For Example If Current Month Is January

if the current month is January then the function should pick A from the $alphabet and place to the 5th position of the random password generated?

For Example If Current Month Is February

if the current month is February then the function should pick B from the $alphabet and place to the 5th position of the random password generated

PHP CODE

function randomPassword() {
$currentmonth = date('F');


if($currentmonth == 'January'){
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ";    
$random = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array();    

     $alphaLength = strlen($random) - 1; 
    for ($i = 0; $i < 5; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $random[$n];
    }

   return implode($pass); 

} 
}
echo randomPassword();
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    Any particular reason why you want to base it on monthly names? It makes it less random and more guessable. There ar far better ways to generate random strings than this. – John Conde Jan 12 '19 at 15:34
  • @JohnConde because it is project requirements? –  Jan 12 '19 at 15:36
  • I'd generate 2 seperate ramdom strings, then concatanate them with the "a", "b",.. in the middle. – Jeff Jan 12 '19 at 15:36
  • @Jeff example code? –  Jan 12 '19 at 15:36

1 Answers1

0

To do this all you need to to is keep track of the month and use it to get the nth character of your $alphabet string. Then you can just replace the fifth element of your $pass array with it before you implode() the final password.

function randomPassword() {
    $currentmonth = date('n');
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ";    
    $random = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array();    

    $alphaLength = strlen($random) - 1; 
    for ($i = 0; $i < 5; $i++) {
          $n = rand(0, $alphaLength);
          $pass[] = $random[$n];
    }
    $pass[4] = substr($alphabet, $currentmonth - 1, 1);

    return implode($pass); 

}
echo randomPassword();

Demo

Here's a more concise way to generate the random password:

function randomPassword() {
    $currentmonth = date('n') - 1;
    $alphabet = "abcdefghijklm";
    $random_hash = substr(md5(uniqid(rand(), true)), 28, 4);
    return $random_hash . substr($alphabet, $currentmonth, 1);
}
echo randomPassword();

Demo

Keep in mind these passwords are short and pseudo-random and not very secure.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I have to try your code is showing like `fg1YYm` why last alpha `m` it should `A` –  Jan 12 '19 at 15:43
  • Check again and view the demo – John Conde Jan 12 '19 at 15:47
  • ok, it is working with `5th position` ok how about if the password is `15` character and I want to change `5th` and `10th` Position? –  Jan 12 '19 at 15:51
  • Here the requirement 5th character will be an alphabetic letter, remember this alphabet starts from A and will end at Z. 10th character of the password will also be an alphabet but in the reverse order, example: first month the letter will be Z The last 15th character will also be an alphabet but in the reverse order i.e. Z –  Jan 12 '19 at 15:52
  • I think my example shows you the concept of how to do that. – John Conde Jan 12 '19 at 15:53
  • As you only want 1 char and the source is a string, you can treat it as an array and just use `$pass[4] = $alphabet[$currentmonth - 1];` – Nigel Ren Jan 12 '19 at 17:35