When writing a program, it is important to have the abillity to gather input from your users. Computers don’t really think for themselves, and they don’t read minds (which is good, because I mentally cuss at mine all day) so it is important to tell them what you want. We will use a simple example. How about http://www.amazon.com? More specifically, when you click to view the details of a particular book, you get a page that displays the detailed information about that book such as author, title, publisher, maybe a picture of the book, some comments from people who bought the book, a short description of the book, some reviews from professional reviewers, and the bank account of the guy who created amazon.
These pages that display this book information aren’t static. Every time you click on a different book, you actually get the same page. The only real difference is the book information pertains to the book you clicked. How does it know the difference? How does it know you want book A instead of book B? Lets look at an HTTP URL for some answers.
The first part of this URL is the HTTP. HTTP specifies the protocol to use (posibilities include FTP, HTTP, LDAP, etc..). All web traffic (and I mean web pages, not email and such) travels over the HTTP protocol. It simply is the mechanism to get a page from one location to the other. Referring to our last discussion on output, the HTTP protocol would ship the output generated by our script to the browser.
The next part of the URL is the computer where the information is kept. In this case, it is www.cmagic.biz. This computer is the actual web server. This may or may not include the leading www. There are no real rules as to how many parts can make up a computer name (www.cmagic.biz,
abc.efg.hijk.lmnop.qrs.tuv.wx.yz.cmagic.biz; all valid names for computers). The few rules that do exist for computer names include rules on special characters. Numbers and dashes/underlines are ok, but not any other special characters. This means that when we hit the first / (other than the two after the protocol – ://) that we know we have moved from the name of the computer to a folder or file on that computer.
Once we hit the /, things change a bit. From here on out we are dealing with a file/folder path on that computer. For instance, if your web server was pointing to the c:\www directry on your hard drive, then the url path (the whole http://www.cmagic.biz portion is left out to avoid carpal tunnel
syndrome on my part) /index.php would be looking for the index.php file
located in the c:\www folder on your drive. If the url path was
/myfiles/index.php, then it would look for a sub folder of myfiles in the c:\www folder and then try to locate a file called index.php in that folder.
Lastly, after the folder/file path, we see a ?. This doesn’t always have to be there. The ? signifies that input is coming. A web page doesn’t hook directly to your keyboard, and it doesn’t directly hook to your screen.
Because of this, some ways had to be devised that would allow you to tell the script (page) what you want. After the ? is a name=value pair. You can send any name=value pair that you want. You can even send multiple name=value pairs if you seperate them with a & symbol. These symbols just give clues as to what is coming next (e.g. input data comes after the ?). To tell amazon which book you want to look at, there is a whole URL (with some extra information in it) and at the end is ?n=283155. The number is some internal indentifier that is assigned to that book. Often in the case of books, an ISBN number is appropriate. One of the things you might find on a URL is that some items are encoded (%20 is a space). This is so that characters can be put on the URL without messing up the order of things (what if you use the = sign in your value?).
Ok, now that we have talked about urls, lets look at a script.
Upload this file to the server, and type in your URL to access the file. It should say “Your name is:” and end there. To actually have it display your name, add a ?name=bob on the end of the url and see what happens.
PHP is great because it does much of the tedious stuff for you, like when your script runs, it has already pulled all the information from the URL for you. This information is placed in an array. You can refer to any Name=Value pair that was passed to the script simply by substituting the name. For example, what if we changed things to first and last name?
Now change the URL to ?first=bob&last=smith. Notice that the in the array, we use first for the first name and last for the last name (same as on the URL). All variables (this includes arrays) starts with the $ sign. By using the  we specify an item in an array. PHP supports keyed arrays which allows us to specify a slot by name (versus numbers). We will talk more about arrays later. Just know that you can get the value of any item on the URL by using $_GET[“your value name”]. It is inportant to put the name inside the quotes.
That is the basics of supplying input to a script. The other main wait to supply input to a script is via a POST rather than a GET. When you see stuff on the URL, it is almost always a GET. When you see data on the URL (stuff after the ?) it is always a GET. We will discuss this topic more in the
future. From the programming perspective, there is no real difference for retrieve information sent via a POST than retrieving information send via a GET. All information supplied on the URL is accessed through the $_GET array, and all information submitted via a POST is accessed through the $_POST array. Pretty simple.
One last note, when you don’t care where the information comes from (via
POST or GET), then you can use the $_REQUEST object. It is a combination of POST and/or GET variables, so it won’t matter which way the data was sent. We will demonstrate POSTing information in another tutorial.
Computer Magic And Software Design