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.





33 Comments on "PHP Query String"
Great article. I will try now. thanks for sharing.
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????
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.
How to assign the $_GET['a'] to an variable?
$a = $_GET['a'];
will it work? I am getting exception. Please advice.
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.
Good Article
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.
To link to the specific page from Google, your swf needs deep linking capability. ,
Great blog! Bookmarked!
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
Great article
Can u help me in coding php including query string for submitting a query to SRU via URL remote server for example library of congress(http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve‘ using the following search fields:
title
author
keyword
thanks in advance
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
Jene i am not sure what you mean? You can access POST variables from $_POST array.
How is the $_SERVER['QUERY STRING'] used to check access a url in a form of page.php/?/1/2/3
Is possible to save the query string values in a array?
No need for it, they are already in $_GET array.
Thanks for the help. I’m working on a search program for my site.
Thanks Again.
thank you but who i can reset query string?
thank you
if you type in to the browser
http://someurl.com/page.php?a=1&b=2&c=3
u will be taken to
http://someurl.com/1/2/3
Fine, My question is;
but what if the user types
http://someurl.com/1/2/3
will he be taken to the folders 1/2/3 instead?
@Eric, no, it works the same for everyone.
without using .htaccess but i need the url like this http://someurl.com/1/2/3
hi greg
plz
without using .htaccess but i need the url without question mark
===================
refering_page.php
===================
===================
query.php
===================
<?php
$query = $_SERVER['QUERY_STRING'];
$chars = '[=,+]';
$replace = ' ';
$terms = preg_replace('/' .$chars. '/', $replace, $query);
echo $terms;
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
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!!!
@Yiannis, you can do that with:
$id = $_GET["articles"]
echo $id; // 11
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!
http://someurl.com/page.php?a=1&a=2&a=3
if my variable in querystring is a as common. how to print values of a.
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
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
Cheers, Greg. You’ve saved me tons of time with this tip.
Gotta learn php, just got to…
Hi,
How we make multiple pages through one page using query string inPHP.