Note that this URL encoding is only needed with GET requests, since they use URLs to send data. The string we wanted, Stage Two Turbo & New Suspension, is safely nestled inside of the $mods variable with no ill effect. The second link however uses the $mods variable after it has been massaged by the urlencode function and therefore does not break our application. => Stage Two Turbo & New Suspensionĭo you see the difference? The first link completely breaks the data! It has turned the contents of our $mods variable into two entirely different variables! This is sure to rain fire from the heavens upon your web application. This is going to cause some unexpected results! To illustrate the problem, we created two links, one with the $mods variable left untouched, and the other with our $mods variable run through the urlencode function. Do you see a problem here? That’s right, we’re placing a reserved character, the ampersand, in the string that we want to pass as a variable in the URL. The string is Stage Two Turbo & New Suspension. So what we are doing here is adding a string to our $mods variable. Let’s add a variable to the list of variables that we’d like to send via the query string. We can see this in action and why this is necessary by breaking our original link.
#Php url encode for a href plus#
Spaces become plus signs, while letters, numbers, underscores, and dashes go through unchanged. By running the query string through this function, it will convert any of the reserved characters to their percent / two digit hexidecimal pair to make your life easier. This here handy function is going to do the dirty work for you. Don’t memorize all the characters, just remember the functions to deal with them. Just Kidding! You do not have to memorize this table, there are much better things to do with your time! In fact let’s start making good use of that time by talking about the built in functions in PHP that will do this for you. Ok, we know there are quite a few characters to be aware of, so go ahead and memorize the table above, then come back once you’re ready to continue working with URLs in PHP. The solution is that instead of using the actual character in the URL, you should use the the equivalent percent sign hexadecimal digits encoding value in the URL. So as you can see, there are quite a few characters that could cause problems for you.
This table contains the reserved html characters we need to be aware of. In HTML there are some reserved characters that we must be aware of because if we include them in a query string without the proper encoding, the URL will break. Working With GET and Links in PHP Page OneĬool! By passing in values to the variables in the query string, we are able to pass data from one page to another via the GET Super Global variable. One with a link to page two, and the other with some logic to dump the contents of the GET Super Global Variable to the screen.
#Php url encode for a href how to#
The classic example of how to explain the way the GET Super Global and html links work together is by creating two html pages and creating links between them to show how the data is passed.
The query string is the part of the URL that comes after the domain. When we’re working with URL’s and Links in PHP, we can pass variables via the URL itself in the form of a query string. Let’s jump right in to working with URLs, Links, and PHP. The concepts here are in use all over the internet on virtually every website you’ll ever visit. Some other key points will be to learn what the reserved characters in URLs are, what characters are unsafe, and when and how we’ll need to encode them for proper use. Specifically, we’ll need to learn what a $_GET variable is, and how it works in combination with URL parameters. In this PHP Tutorial Series, we’re going to take a look at how to generate links using PHP so that we can include variable data via URLs and Super Globals. As we have seen, when working with PHP, it is going to be common and necessary that we process data and information using variables. We’ve covered a lot of technical detail concerning dealing with hyperlinks in HTML here at VegiBit.