Join Kevin Skoglund for an in-depth discussion in this video Add random numbers and symbols, part of Easy PHP Projects: Password Utilities.
- In this movie, we'll add randomly selected numbers and symbols to our readable password generator project. Let's start by picking a random symbol. Now we already saw how to pick a random item out of an array. And we certainly could just make our symbols into an array and then tell it to pick one of those randomly. But I think it's actually a little simpler if we do it as a string instead. So we'll have function pick_random_symbol() And we won't have any arguments to it. We're just going to pick a symbol.
And so we'll define our list of symbols. And we're going to make this $*?!- You can use any symbols that you want. But those are the ones I'm going to use. This is the same thing we did in the last chapter. I'm going to pick a random index from those, just like we did last time, and return it. I'm going to copy this, come down here and paste it. The one difference is that I'm not working with an array anymore. I'm going to be working with a string which is called $symbols.
So let's copy that and replace array. And we don't want to use count because count is for counting elements in an array. If we're working with a string, we want to use strlen for string length. That will return the length of the string, subtract one from it, and return an index position. And then, strings work the same way as arrays in that if we ask for that index position, then it will return just that single character to us. So that will now return a random symbol. Let's come down here. Instead of pick_random($words), pick_random_symbol().
Let's try it out. Let's go into Firefox. Let's reload the page. There we go. stamp*question halt!radiation not?mind All right, see how it works? It makes it easier to read the words when we divide them that way. Now let's add numbers as well. So right after we have pick_random_symbol, let's have function pick_random_number. And we're going to tell it how many digits we want it to pass in. By default, it will be one.
Now we could do this in a similar way. We could do mt_rand and pick a number between zero and nine. And we could then loop through that. And we could loop through it however many times we've asked for digits. If we did, it might look something like this. You can see that in the middle of the function, I've got mt_rand from zero to nine being called. I'm also converting it into a string. As I go through the loop, I'm appending it onto my original string, which is the empty string for output, and returning a string at the end. Why am I working with a string? Well, notice up in the comment at the top there where I give the example number, 00836.
It's possible that we could have a number that starts with zeros. I call it a zero-prefix number. If this were an integer, then those numbers would just disappear. And it would just be 836. In order to preserve those prefixed zeros, we need to do something different. We need to turn it into a string. However, I happen to think that those zeros in front of it aren't very friendly. So I'd like to get rid of them altogether while still preserving the number of digits. Now there are some ways we could do that. For example, you could just tell it that the first time through the loop it should use a number from one to nine, and then after that, every number is zero to nine.
But I'm going to write code to address it in a slightly different way. Instead, what I'm going to do is I'm going to have a minimum value which I'm going to compute by taking the number ten and raising it to the power of the number of digits that I have minus one. Okay, we'll see why that is in just a second. And then, I'm going to also compute the maximum number. And that's going to be achieved by raising ten to the number of digits.
And then after I've raised it to that number, minus one. Okay, now if you want to stop and think about that for a second. If we ask for a number like four digits, right? Then that's going to say four digits minus one would be three. Ten raised to the third power would be 1,000. And then when we're creating the max, we would take ten to the fourth power which would be 10,000. But then we're going to subtract one from it to get back to 9999.
So that's how I'm going to get the numbers for the maximum and minimum. And then I could just ask mt_rand to return the minimum and the maximum value. Now I'm also going to put that strval on it. That will just make sure that it returns a string value as well. That's really optional. You don't have to. But I am going to be appending it down here. So let's try using it. Pick_random and let's say _number. And how many digits do we want? Let's start by having three digits. So now we should get a random word, a symbol, three digits, and another word.
Let's save it and let's go try it out. We reload again. port-489withdraw Let's reload the page and again and again. And you see that it does work. We get a random word, a random symbol, three random digits, followed by another random word. So already, we've got a working, readable password generator. In the next movie, I want us to look at how we can start configuring the length of the password that's returned.
- Building a randomized string with PHP
- Allowing password configuration
- Choosing random words from a dictionary
- Picking words of specific lengths
- Creating a rating function to rate password strength
- Using CSS to display a color-coded strength meter