Getting good with php

Page 81

81

Chapter 4

Andrew+Burgess. Notice the attached query string. Now, the name parameter is set, and so I see the message.

Note that there’s nothing special going on behind the scenes with the query string. You could just type the URL http://localhost: 8888/ggwp4/?name=Sherlock+Holmes into your browser and you’ll get the message. (You know, of course, that these URLs would work just the same with index.php in there, after ggwp4/.) It’s really that simple to make a GET request with parameters and use them with PHP. What’s this good for? Well, since you can see the parameters right in the URL, you don’t want to use it for anything private, like logging a user in or changing data on the server. But, any task where you want to take some not-so-private data from the user, GET is great. Here are some usage examples, coupled with example query strings: • signing them up for an email newsletter: name=Andrew&email=andrew@example.com • asking their location so you can provide the appropriate content: zip=12345 • taking search results: query=learning+PHP • showing a specific product: prodid=123 or product=pc-12 You aren’t familiar with the POST request yet, but know that the data it passes to the server is not passed as part of the URL, but more discretely in the HTTP headers instead (I’ll explain more later). Of course, this is why it’s better for sensitive data. However, there’s a benefit to having the data in the URL: it makes them bookmark-able. More on this in the POST section. It’s common for beginners to get all excited about the idea of having dynamic user input, and take the idea too far. For example, there’s technically nothing stopping you from having a single


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.