Skip to content

PHP Query String

Query string plays important role in building web applications, especially if you want to make nice urls without question mark and many key, value pairs, actually it is not so easy to design application with nice urls however it is always worth doing so, because it not only looks very proffessional, such URLs are search engine friendly, which means that they will get indexed faster but also it happens that search engines have problems with indexing pages with more then 3 key=value pairs in query string.

However, one place where using nice URLs is not necessary are all kinds of admin panels, there are usually only few admins and more over admin panel never gets indexed so it doesn’t make any sense to make those URLs search engine friendly.

Query string can be accessed thru global array $_SERVER, more specific $_SERVER[‘QUERY_STRING’] is the actual variable where query string is written, this variable contains all data that is inputed after question mark in the URL. For example if we have URL which looks like this:
http://someurl.com/page.php?a=1&b=2&c=3

Then echo $_SERVER[‘QUERY_STRING’] will display: a=1&b=2&c=3, such data is no use to us, we need to parse it, or get it thru global array $_GET, in our case we could write:

echo $_GET['a'];
echo $_GET['b'];
echo $_GET['c'];

which would output:

1
2
3

Two things i have to mention here:
– i do not recommend displaying variables passed by user withut checking if they contain potentially dangerous code.
– make sure you have register_globals set to off in your ini file. If you do not have access to ini file, you can change this setting in your .htaccess file (if you have htaccess files working on your server). To do this just add following line php_flag register_globals off.

Using $_GET array to access variables from query string is pretty simple, however we would want to transform our URL to make it look like this:
http://someurl.com/1/2/3
This is very search engine friendly approach, however like always there are no roses without thorns. First problem is that $_SERVER[‘QUERY_STRING’] variable is empty now, so $_GET array is empty as well.

But first thing first, we can’t just input this URL in web browser address bar, hit enter and wait for a page to load. Probably it would end with 404 Error. To avoid this we need to create .htaccess file in the same directory as page.php file. Then open .htaccess file and write in it:

RewriteEngine on
RewriteRule .* page.php

Now all requests to directory which contains file page.php will be redirected to page.php file. Pretty simple and we can now use URL without question mark or any other unwanted signs i provided earlier (e.g. http://someurl.com/1/2/3).

So as i said earlier now our $_GET array is empty, but fortunately we can still check what the URL looks like by writing this code:

echo $_SERVER['REQUEST_URI'];
// output /1/2/3

Well, maybe URL looks pretty search engine friendly however id does not provide a lot of info as it did earlier. Unfortunately solution to this problem is beyond the scope of this article and i will give you only few guidlines: first you need to parse URL wit explode() to get array, then it will be good to assign some keys to each of the variable, but you always need to remember who is who, do not change order of parameters in the URL.

I covered only two types passing arguments to application with query string, there are other solutions as well, but i think that this two are the most common used and are actually the best when it comes to PHP.

Published inGeneral

35 Comments

  1. xyz xyz

    Very Useful article.I require more information on this. Like how the url of this article http://ditio.net/2008/06/12/php-query-string/ worked. I guess we need to write the code in index.php get the query string and then proceed. Am I right????

  2. You are close to it @xyz. If you want to such “nice” urls like on this blog then you will need to do a search on ReriteEngine there are some good resources on the web, but unfortunately as for now there none on my blog.

  3. jarrelva jarrelva

    How to assign the $_GET[‘a’] to an variable?

    $a = $_GET[‘a’];

    will it work? I am getting exception. Please advice.

  4. Yes it should work assuming you have …&a=something in your url.

    Such code doesn’t throw exceptions i think you are getting some kind of notice, it would be helpful if you would post this error here.

  5. juvy juvy

    Good Article

  6. Chris Chris

    If you create the .htaccess file like this
    RewriteRule ^page/([0-9]+)/([0-9]+)/([0-9]+).html$ page.php?a=$1&b=$2&c=$3

    You can still use $_GET so won’t need to change the code in page.php at all.

  7. To link to the specific page from Google, your swf needs deep linking capability. ,

  8. thanks for the advice on this. ive just written my own blog software and im trying to remove query stings. ill go though this and see if i can implement

    cheers

  9. lighten-up lighten-up

    Great article :)

  10. Very well explained and truly a great article. However, could you please suggest how POST variables can be treated in a php script? I am sorry, may be a dumb question but I am a beginner.

    Thank you

  11. Jene i am not sure what you mean? You can access POST variables from $_POST array.

  12. How is the $_SERVER[‘QUERY STRING’] used to check access a url in a form of page.php/?/1/2/3

  13. Is possible to save the query string values in a array?

  14. thank you but who i can reset query string?
    thank you

  15. anu anu

    hi greg
    plz

    without using .htaccess but i need the url without question mark

  16. ===================
    refering_page.php
    ===================


    ===================
    query.php
    ===================
    <?php
    $query = $_SERVER['QUERY_STRING'];
    $chars = '[=,+]';
    $replace = ' ';
    $terms = preg_replace('/' .$chars. '/', $replace, $query);
    echo $terms;

  17. Issac Issac

    I’m trying to pull the values from one form to the next.. like if i have a mini form with

    Name:
    Last Name:
    Email:

    I want the user to go to the next page when they hit submit linking the info they just entered into the new form….
    With the fields all ready filled out….

    Name:alex
    Last Name:gomez
    Email:irocksocks@yahoo.com

  18. Hi,

    how will get from
    http://christoscler.com/viewarticles.php?articles=11

    the id number 11??

    when i insert this address into to the address bar it doesn’t show me the data that i have stored in the db.

    Basically i am trying to get number 11 from address to give the value to the $id.
    THANK YOU IN ADVANCE!!!

  19. @Yiannis, you can do that with:
    $id = $_GET[“articles”]
    echo $id; // 11

  20. Ian Ian

    I have several parked domains on my VPS that have been ‘wasting’ away. I decided to use Google’s “Adsense for Domains” and one of the options is to put the domain up for sale. If you choose this option, you have to give it a URL to send people to for info.

    When you do this, you can use a wildcard (*) to append the domain name at the end of a query string. With your post, I was able to put a template on my VPS that calls for a query string in several places which makes the website look customized to whatever domain people clicked in from. Thanks for your post!

  21. I have tried this way, however, I still have a problem that how to change the question mark in the URL when I turn into the next pages, such as .html?=2

  22. I want to pass the form data to another page using get query string but am not getting the correct syntax as in Issac’s comment

    I’m trying to pull the values from one form to the next.. like if i have a mini form with
    Name:
    Last Name:
    Email:
    I want the user to go to the next page when they hit submit linking the info they just entered into the new form….
    With the fields all ready filled out….
    Name:alex
    Last Name:gomez
    Email:irocksocks@yahoo.com

  23. Cheers, Greg. You’ve saved me tons of time with this tip.
    Gotta learn php, just got to…

  24. sandeep sandeep

    Hi,

    How we make multiple pages through one page using query string inPHP.

  25. Deepanjan dey Deepanjan dey

    Thanks, good article

Leave a Reply

Your email address will not be published. Required fields are marked *