Passing variables using in the URL

Daniel Pataki
Mar 7, 2009
Updated • May 26, 2017
Development
|
5

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!

Passing variables using in the URL

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: http://url.com/page.php?variable=value&variable=value. 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!

Summary
Article Name
Passing variables using in the URL
Description
The guide explains the concept of using variables in urls and PHP.
Author
Publisher
Ghacks Technology News
Logo
Advertisement

Previous Post: «
Next Post: «

Comments

  1. Daniel Pataki said on March 8, 2009 at 12:25 am
    Reply

    Hi eRIZ!

    Indeed, I did, but I just wanted to show why this is useful, there are a TON of things that can be added (not just sanitizing), but I need to keep some stuff to myself for upcoming articles too :)

  2. Chaney said on March 7, 2009 at 2:09 pm
    Reply

    Mmmm, interesting function and thanks for sharing that site.

  3. eRIZ said on March 7, 2009 at 12:37 pm
    Reply

    You didn’t have mentioned about very important thing – to sanitize URL variables what is forgotten by many begginers…

  4. Daniel Pataki said on March 7, 2009 at 10:50 am
    Reply

    Thanks, and boy was I agree in the morning because of that :D

  5. Rarst said on March 7, 2009 at 10:06 am
    Reply

    Outgoing link for blog is broken.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.