Passing variables using in the URL

Status
Not open for further replies.

Osiris

Golden Master
Messages
36,817
Location
Kentucky
Source: Passing variables using in the URL

If you've seen some Wordpress blogs or online stores you've probably seen url-s in the form of http://somepage.com/product.php?id=4. These sites pass variables to scripts using the url. The script will scan the url for these and you can use them in a database query for example. This is the technology that allows Wordpress templates to function, since you only need one page for all your posts.
Instead of creating a separate HTML page for each post we make, we enter our posts into a database. Each post is then assigned an id. We simply create one page, say posts.php and the code for that page contains a query, plus all the necessary HTML for displaying a post. This query will pull one specific post from the database and put the correct pieces where they should go in the template. We can tell the script which one to pick by passing it a variable in the url. If the value of this variable is 135 for example, the post that will be pulled will be the one with this id.
You can see right away how valuable this is, you can build truly dynamic pages with this method. Your data is better organized in a database, and you can change the way you display it by simply modifying the HTML code, no need to recreate any content whatsoever. So how is this done on a code level? Let's take a look!

You need to use a PHP file obviously, and after the extension you need to write the names and the values of the variables you want to pass in the following format: url.com: Search with Many. Notice that initially we use a question mark to separate the variable name passed from the extension, but after each subsequent variable passed we use an ampersand.
You can easily retrieve these variables in the code, referencing them with $_GET['variable']. For example, let's say the url you use is the following: http://scriptastique.com/post.php?post_id=345. In this case inside the PHP file the value of $_GET['post_id'] is 345.
Using the ‘get' method you can pass strings, not just integers, so you could pass the name of the referring url, the name of a user, whatever you need. You should be aware though, that using the get method is unsafe if you want to send sensitive data. For example, you wouldn't want to pass a user's password from one page to another using the get method for many reasons, but mainly because it is displayed in the url bar.
For other things like telling a script which post you want to show, or which category you want the description for it's fine, and a very comfortable way of organizing a huge site into just a few files, in fact, I think this is one of the best features of PHP!
If you liked this article, perhaps you'd like to take a look at Scriptastique, which is a blog (and upcoming tutorial site) aimed at professional and aspiring coders. We just started, but there are daily posts and the tutorials (along with screencasts) are on the way!
 
Status
Not open for further replies.
Back
Top Bottom