faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input : URLs and Post or Get Methods

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

44 of 61 people (72%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

How can I get the part of a request after the question mark?
How can I get the query part of a GET request?

Feb 4th, 2006 06:51
Andrew Mather, Nathan Wallace, Onno Benschop, George Brown, John R. Levine


PHP will automatically parse the URL request and set the variables
specified. [*** NOTE: NOT IN PHP VERSIONS AFTER 4.2.0 *** see below]

    http://whatever/blah.phtml?foo=1&bar=2
    // $foo is 1
    // $bar is 2

Sometimes though you may be interested in using the actual value of the
variables string.  This is available as:

    // $QUERY_STRING is foo=1&bar=2


[see also eg: echo($_SERVER['QUERY_STRING']); ]

*** NOTE: ARGUMENTS ARE NO LONGER AUTOMATICALLY AVAILABLE as of PHP 4.2  ***

See: http://uk2.php.net/manual/en/language.variables.external.php

KEY SECTION IS THIS: -- Note PHP directive register_globals --

<?php 
// Available since PHP 4.1.0

   echo $_POST['username'];
   echo $_REQUEST['username'];

   import_request_variables('p', 'p_');
   echo $p_username;

// Available since PHP 3. As of PHP 5.0.0, these long predefined
// variables can be disabled with the register_long_arrays directive.

   echo $HTTP_POST_VARS['username'];

// Available if the PHP directive register_globals = on. As of 
// PHP 4.2.0 the default value of register_globals = off.
// Using/relying on this method is not preferred.

   echo $username;
?>