Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we know how to construct URLs to be used as links, we need to talk about the characters that we're allowed to use in those URLs. URLs can contain the most common characters like letters, numbers, underscores, and dashes. But there are some characters that are going to cause problems because they have special meaning to the URL. These are known as reserved characters. Here are a list of the reserved characters that we have problems with if we include them in the URL. You can see there are quite a few. When we're constructing URLs for lengths, we must encode these characters so they don't interfere with the function of the URL.
Encoding a reserved character means converting that character to a percent sign followed by a pair of hexadecimal digits. Hexadecimal just means that in addition to zero to nine, we can use the letters A through F as if they were digits, too. So, what we'll need to do is be able to convert each of these characters into its hexadecimal form, put it in a URL. And then we'll decode it by converting it back once the page is received. PHP lets us perform the URL encoding by using a function called urlencode, and its argument is the string that you want to encode.
So, urlencode is going to take letters, number, underscores and dashes and let them pass through completely unchanged. But those reserve characters, that we just saw, are going to become their hexadecimal equivalent. That percent sign, follow by two digit hexadecimal digit number. And spaces are going to become plus signs. Remember that, because that's going to be important in a minute. We'll talk about that. Let's try using it. So, let's go back to our first page. In addition to sending ID 5, let's also add another one here. Let's do an &company equals and we'll put a company name. We'll put PHP echo $company, there we go.
And then, up here, we're going to provide that company name. $company equals and that company name could be any string. So let's imagine that we're looking at a directory of employees lets say. And we're going to look at a company called Johnson & Johnson. Do you see the problem here? It has an ampersand in it. So, let's just try it on its own. Let's save it. Let's go back to first page so that we can see what the problem is, I'm just going to go first page.php. Alright. It generates the link just fine, but when I click on it gives me a hint what it's going to do down at the bottom. When I click on it you'll see up here that the URL is Johnson space and space Johnson.
Let's go over to our second page, and let's echo that value. So, I'm just going to copy this, for now. Paste it, and we'll just change it to company, and company, and company. So, it's going to get that company value out of it. So, reload the page, Look what I got, Johnson. That's the company, because it saw this ampersand, and it said, oh, that's a new query parameter. That's what's used to divide those. So, you see what the problem is now. Now, the solution is to use urlencode. So, what we do is we provide urlencode right here, urlencode, this string.
Make sure that you use echo with urlencode, super important. This will not do it for you. This will encode it, but then it'll throw the value away. Nothing will happen with it. You need that echo there. So, make sure you've got that. Echo, the urlencoded value of company. Let's save it. We'll hit the back button. Go back to first page, I'll just reload that page. Now when I click on it, now look what it gives me up here. Johnson a plus sign for the space, and then the ampersand became % 26. And then when I pulled the value back, it correctly found it. It correctly changed the ampersand back, converted those spaces back as well. Now, we didn't actually do any decoding.
It's because PHP automatically does the decoding for us when it assigns those values to the get superglobal. It's kind of assumed the values are encoded and ready to be used. In which case they need to be decoded so that we can use them. So, it just does it automatically for us. We don't actually have to decode. There is a function called urldecode that works exactly like you would expect that you can use if you ever need it. Must most times you don't need to. Now I just want to insert a little parenthetical here that this is just for get requests only.
Later when we start working with posting cookie, we won't need to do this encoding. This is just for GET requests because those values are in the URL string at the top. So we're URL encoding them. So, now that we know about URL encoding, there's another type of encoding that we need to talk about called raw URL encoding and it's important to understand and to know the differences. So, if we take a look at what we had for urlencode, we saw that letters, numbers, underscores, and dashes are unchanged. It converts the reserved characters and spaces become plus. With rawurlencode, just put the word raw in front of it, no spaces or underscores, just all smashed together, letters, numbers, underscore and dash are unchanged.
The reserved characters get converted but, here's the big difference. Spaces become percent 20. They get encoded into a two-digit hexadecimal also instead of using the plus. It may seem like a minor point, but it does make some big differences. It's easy for us to try it. We'll just pop back over here, and we'll just type raw in front of urlencode. Now we'll come back here. We'll go back to first page, we'll reload the page. And you can see now, when I click on it, now it is Johnson space Johnson with the percent 26 encoded. So, now you may be thinking well wait a minute, I thought you told me that it was going to convert this space into percent 20.
It actually did. Let's go back real quick to the first page and let's view. Page source, here we go. See, here they are. Percent 20, percent 26, percent 20. That's what the link was on this page. Then when I clicked it, and it shows up in the URL. Firefox is helpfully converting those back to spaces for me. But not all browsers do that. It is percent 20. That's what you're actually sending. So, now that we understand the difference. One send pluses, one sends percent 20. You're probably wondering, when should you use each one? Well, here's my guideline for you.
You're going to want to use rawurlencode on the path, that's the portion that comes before the question mark. So, everything comes before that, if there's anything dynamic in there that you're generating, like the page name. That is all going to be done with raw URL encoding, because the spaces must be encoded as %20 for the final system to be able to find that file. For Apache to be able to locate the PHP page you're looking for, it needs it as %20. But, you want to use urlencode on the query string. That's everything that comes after question mark.
Let's make ourselves a quick page that can demonstrate this. I'm going to come back over here, and I'm just going to take first page, and I'll do a quick Save As on it. I'm going to call it urlencode.php, and let's call it urlencode. We'll make it this way. And here in the body I'm just going to paste in an example. And I've got a page that's going to be used as a bio, so I'm looking for bio for William Shakespeare with a space in it. I'm going to rawurlencode that and then I'm going to ask for a quote that's going to be the parameter I'm going to send of to be or not to be. And I'm going to urlencode that.
Let's just bring that up and take a look at it real quick. So, make sure that you save it. And then, let's do urlencode. And there you go. See the percent 20 there, versus to be or not to be. This, the file system will be able to find for you. If we, instead, flip those around. Make link two. And let's just swap these. Let's make this one raw. And this one is not raw. And then we'll just echo back like two for contrast. Now the file system is going t be looking for something called William plus Shakespeare.
A literal plus sign not a space. So, that's not going to find the file to begin with. And then this to be or not to be. Probably will work out for us okay. Usually, most web servers and PHP can sort that out. But this is the better way to do it. This is going to look better, it's going to work better all around. Rawurlencode, everything that's on this side of the question mark, urlencode everything that comes after it.
Get unlimited access to all courses for just $25/month.Become a member