Tuesday, 6 March 2018

PHP08.1 Example: Lightspeed with Links (GET Requests)


Let’s take a look at how we can provide the user with the ability to choose the number of days that the calculation for lightspeed will be based on. I’m going to start off by removing some of the code that I put in in the last example. I’m not going to create a PHP variable here and in my link that will take me over to the lightspeed page I don’t actually want to include the days here that I just removed. I think I’m also then going to go ahead and take this hyperlink that I’ve created and I’m going to instead turn it into an unordered list. Let me make this an unordered list. I’m going to put a list item element inside of it. I’m going to take the hyperlink that I was using before to go to the lightspeed page and I’m going to make that first list item.
What I think I’ll then do this instead of having the user just generically see the text go to lightspeed, go to the lightspeed, I’m going to put the number one there and I’m going to with that particular link include days=1 as my URL parameter. I’m going to then duplicate that a few times. Let’s also do a days=7 where I’ll show the number 7 to the user and use 7 as the value for the days parameter. Maybe I’ll do a 30 day link, 30 for what the user sees and the days parameter’s value. And let’s do a 365, how far does light travel in a year. Something along those lines. If I jump back over to my browser and take a look at that now what I’ve essentially done is I’ve given the user a number of different options that they can click on so that they can select the number of days.
If I click on the number 1 they’re taken over to the lightspeed page and the calculation is based on the number 1. If I go back and I click on the number 7 we go to the lightspeed page and now the calculation is being based on the number 7, different number of miles. 365? There we are. So I’ve been able to, using this technique, provide the user with a number of different options. Now, I would say that this is a fairly pivotal moment in us learning about PHP because this is really when PHP starts to be dynamic, starts to actually sort of earn the work that goes into it in a lot of ways because what we’ve essentially done at this point is from the user’s point of view we’ve made it appear that there are essentially five different pages: there is the page where they’re able to select the number of days and then depending on which link they click they get a custom output, a custom page based on which one of those four links they clicked on.
What most users will never realize is that every time they click on one of these links they are actually just being taken to the same page. When I click the 1 I go to the page lightspeed.php and the 1 value is passed. If I click on 365 I still go to the lightspeed page. So even though from the user’s point of view it appears that we have something like five pages, in actuality we of course only have the two. We have the one page where the user clicks the link, effectively deciding what input will be given to the lightspeed page, and then we have the lightspeed page itself which, using the get variable, the $_GET variable that we saw in the last video, we’re able to pick up the actual input that the user provided based on what link they clicked on and we’re then able to customize the output from that page based on the user’s input.
So we’ve created two pages, but it appears to the user that there is a lot more of a custom, multipage experience actually taking place there. It’s this basic idea that I would say is most core to the abilities that we get based on server-side scripting that we wouldn’t have without it. There are ways we could do things like this with something like JavaScript for example, but this is going to continue and get more and more powerful as we go forward going well beyond anything that JavaScript could ever do on its own, allowing us to create all kinds of amazing websites.
Things like Google, things like Facebook, things like Twitter, things like Amazon. None of those different web applications would be possible if it wasn’t for server-side scripting and this is basically the point we’re at in PHP when we can start to begin to see a glimmer of what we’re going to be able to create as we continue to move forward. One last detail that I might point out about this particular technique of passing values as URL parameters is that there is a potential security or privacy issue that could crop up here. Obviously what we’re doing here is fairly limited and of course extremely benign in this case, but one thing that you should be completely aware of, completely conscious of, is the way that these get parameters are passed are always as a part of the URL, which means that whenever I click one of those hyperlinks the actual value that I clicked on, the input, in essence, that I’m giving the browser, becomes an actual part of the URL itself and can be seen here in the address bar.
Because it can be seen in the address bar that means that it is also being added to the browser’s history, it also means that it could be bookmarked. Now, neither of those things are necessarily bad; in some cases they may actually be good. For example, if a user wanted to be able to email to someone else the result of a calculation that would show how far light would travel in 7 days, it would in that case be a benefit that a user could go straight to this URL that specifies 7 days and could share that link with other people so they would see the exact same output. But we want to be very conscious going forward that we don’t ever want to put any private, sensitive, or security-related information into a get parameter because that information would also be not only viewable, visible, there in the address bar to anybody who happened to be watching, but could also potentially end up being a part of the browser’s history or a part of a bookmark which could then become a bit of a security issue.
Something to be aware of as we continue to go forward..
For More Info : Visit Here : http://lightspeedreading.com/

No comments:

Post a Comment