How to get Query String in PHP?

April 30, 2008 · Print This Article

Recently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

 Everything that comes after “?” is a query string, so in this example it will be searchText=some_search_text&pageno=2. searchText and pageno are variables and some_search_text and 2 are values. Query string has this form variable1=value&variable2=value, where each variable is separated by ampersand character (”&”). 

In PHP we can access query string using superglobal variableRecently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

SERVER, which is an array that contains many useful information. To access query string, use below code:

Recently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

Everything that comes after “?” is a query string, so in this example it will be searchText=some_search_text&pageno=2. searchText and pageno are variables and some_search_text and 2 are values. Query string has this form variable1=value&variable2=value, where each variable is separated by ampersand character (“&”).

In PHP we can access query string using superglobal variable $_SERVER, which is an array that contains many useful information. To access query string, use below code:

$_SERVER['QUERY_STRING']

We should be very careful when appending new variables to query string, because as I showed above, some time query string can be empty and sometimes it can already contains some variables, because of this before appending new variable, we should check if we have to append ampersand character (“&”) before or not. This can be done by simple if statement presented below. Below code example also uses $_SERVER["PHP_SELF"], which returns script name

<?php
   $self = $_SERVER["PHP_SELF"];
 
  if($_SERVER["QUERY_STRING"]) {
 
    $finalurl = $self . "?" . $_SERVER["QUERY_STRING"] . 
      "&myvariable=myvalue";   
 
  } else {
 
    $finalurl = $self . "?" . "myvariable=myvalue";  
 
  } 
?>

 

I hope this simple example will help some one.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlinkList
  • description
  • Furl
  • Live
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Comments

57 Responses to “How to get Query String in PHP?”

  1. Edison lau on May 28th, 2008 12:55 pm

    Hi need help geting a variable from an remote address example
    http://www.remoteaddress.com/id=geniqgn

    I need a php code that can store the id inside a variable please help me

    edisonlau90@gmail.com

  2. Krystian Zieja on May 30th, 2008 3:21 pm

    Variables from query string can be retrieved using $_GET array, like in this example:

    $myvar = $_GET["id"];

    Regards
    Krystian Zieja

  3. Alexander7 on July 18th, 2011 7:42 pm

    buy@generic.LEVITRA” rel=”nofollow”>…

    Need cheap generic LEVITRA?…

  4. EDUARDO on July 29th, 2011 12:41 pm

    Buy@Cheap.Abana” rel=”nofollow”>..

    Buygeneric pills…

  5. PERRY on July 29th, 2011 9:35 pm

    Purchase@Abilify.Online” rel=”nofollow”>…

    Buywithout prescription…

  6. ROY on July 30th, 2011 4:26 am

    Buy@Generic.Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>…

    Buywithout prescription…

  7. RUSSELL on July 30th, 2011 11:18 pm

    Purchase@Discount.Acai” rel=”nofollow”>..

    Buywithout prescription…

  8. SALVADOR on July 31st, 2011 5:44 am

    Buy@Acai.Without.Prescription” rel=”nofollow”>…

    Buynow…

  9. EDUARDO on July 31st, 2011 9:37 pm

    Buy@Cheap.Coral.Calcium” rel=”nofollow”>.

    Buynow it…

  10. RONALD on August 1st, 2011 8:02 am

    Buy@Cheap.Coral.Calcium” rel=”nofollow”>…

    Buygeneric meds…

  11. DUSTIN on August 1st, 2011 11:04 am

    Cheap@Coral.Calcium.Online” rel=”nofollow”>..

    Buygeneric drugs…

  12. SALVADOR on August 1st, 2011 1:54 pm

    Order@Discount.Abana” rel=”nofollow”>..

    Buygeneric meds…

  13. LEWIS on August 2nd, 2011 8:01 pm

    Cheap@Abana.Online” rel=”nofollow”>..

    Buynow it…

  14. LESLIE on August 3rd, 2011 6:08 am

    Buy@Cheap.Abilify” rel=”nofollow”>…

    Buygeneric pills…

  15. BRANDON on August 3rd, 2011 3:08 pm

    Cheap@Abilify.Without.Prescription” rel=”nofollow”>..

    Buyno prescription…

  16. BOB on August 5th, 2011 10:33 pm

    Order@Accupril.Online” rel=”nofollow”>.…

    Buygeneric drugs…

  17. BYRON on August 6th, 2011 9:56 am

    Buy@Discount.Accutane” rel=”nofollow”>.…

    Buynow it…

  18. LOUIS on August 6th, 2011 9:32 pm

    .

    Buygeneric drugs…

  19. RUBEN on August 8th, 2011 8:42 pm

    Buy@Aciphex.20mg” rel=”nofollow”>.…

    Buygeneric meds…

  20. CLIFTON on August 11th, 2011 3:11 am

    Cheap@Generic.Actonel.35mg” rel=”nofollow”>..

    Buygeneric meds qmo…

  21. LAWRENCE on August 11th, 2011 5:00 pm

    Get@Actos.Online” rel=”nofollow”>..

    Buywithout prescription dyo…

  22. LLOYD on August 13th, 2011 5:53 am

    Cheap@Coral.Calcium.Online” rel=”nofollow”>…

    Buywithout prescription xiu…

  23. RAY on August 13th, 2011 12:22 pm

    Get@Abana.Online” rel=”nofollow”>.

    Buygeneric drugs zfk…

  24. BRYAN on August 16th, 2011 4:03 am

    Buy@Cheap.Accupril” rel=”nofollow”>.

    Buygeneric drugs nih…

  25. RONNIE on August 16th, 2011 5:49 pm

    Cheap@Accupril.Online” rel=”nofollow”>..

    Buygeneric drugs ajd…

  26. VINCENT on August 17th, 2011 11:49 pm

    Cheap@Accutane.10mg.20mg” rel=”nofollow”>.

    Buyit now ojb…

  27. MARVIN on August 18th, 2011 11:01 am

    Order@Aciphex.Online” rel=”nofollow”>.

    Buynow it lmg…

  28. JOHNNY on August 18th, 2011 1:27 pm

    Order@Discount.Aciphex” rel=”nofollow”>…

    Buygeneric meds svb…

  29. BERNARD on August 24th, 2011 7:43 am

    Cheap@Advair.Without.Prescription” rel=”nofollow”>…

    Buyit now sge…

  30. ALVIN on August 25th, 2011 12:28 am

    Buy@Cheap.Aggrenox” rel=”nofollow”>..

    Buygeneric meds dxy…

  31. RAUL on August 26th, 2011 5:48 am

    Buy@Cheap.Aldactone” rel=”nofollow”>..

    Buydrugs without prescription hak…

  32. BRAD on August 26th, 2011 5:32 pm

    can@you.take.aciphex.and.nexium.at.the.same.time” rel=”nofollow”>..

    Buygeneric pills…

  33. DARRYL on August 27th, 2011 5:31 am

    actonel@and.heart.irregularities” rel=”nofollow”>..

    Buyno prescription…

  34. MELVIN on August 27th, 2011 11:12 am

    aldactone@kidney.problems” rel=”nofollow”>…

    Buygeneric drugs…

  35. CLINTON on August 28th, 2011 7:51 pm

    actonel@and.dvt” rel=”nofollow”>.

    Buygeneric meds…

  36. EVERETT on October 17th, 2011 7:29 am

    levaquin 500mg

    Buy_no prescription…

  37. ALBERT on October 20th, 2011 6:35 pm

    financial aid for alzheimer’s

    Buy_without prescription…

  38. TODD on October 21st, 2011 2:54 am

    heart worm medication

    Buy_no prescription…

  39. ANTONIO on October 22nd, 2011 7:45 am

    blood pressure medications

    Buy_now it…

  40. BARRY on October 26th, 2011 3:26 am

    hepatitis b shot

    Buy_it now…

  41. DAN on October 26th, 2011 11:46 am

    drug trafficking in latin america

    Buy_drugs without prescription…

  42. LAWRENCE on October 29th, 2011 12:19 pm

    permanent removal of adult acne scars

    Buy_generic meds…

  43. KENNY on October 30th, 2011 3:44 pm

    enhancement drugs

    Buy_no prescription…

  44. TERRENCE on October 31st, 2011 3:22 am

    drug test company

    Buy_without prescription…

  45. ERIC on November 2nd, 2011 6:43 pm

    epson salt and lemon juice diet

    Buy_generic pills…

  46. BRYAN on November 2nd, 2011 11:43 pm

    l91 energizer e2 lithium aa cells

    Buy_generic drugs…

  47. JOEY on November 3rd, 2011 1:03 pm

    how to buy glucose meter

    Buy_generic meds…

  48. JEREMY on November 3rd, 2011 6:49 pm

    skin cancer foundation australia

    Buy_generic drugs…

  49. CHRIS on November 5th, 2011 5:07 am
  50. JEFFERY on November 6th, 2011 12:47 pm

    depression medcine xr

    Buy_drugs without prescription…

  51. CHARLIE on November 8th, 2011 7:01 pm
  52. NATHAN on November 12th, 2011 1:21 am

    meal plan for hypertension

    Buy_generic drugs…

  53. RAFAEL on November 13th, 2011 12:41 am

    scabies in dogs help

    Buy_generic drugs…

  54. ALFRED on November 13th, 2011 12:21 pm

    herbals for pregnancy

    Buy_drugs without prescription…

  55. WESLEY on November 17th, 2011 4:59 am

    diet secrets for models

    Buy_now it…

  56. JEREMY on December 11th, 2011 6:24 pm

    best@medicare.plan.d.for.nexium.40.mg” rel=”nofollow”>..

    Buygeneric meds…

  57. HARRY on December 11th, 2011 10:39 pm

    infant@prevacid.dose” rel=”nofollow”>.

    Buygeneric drugs…

Got something to say?

You must be logged in to post a comment.