How to set $_GET variable

114,876

Solution 1

You can create a link , having get variable in href.

<a href="www.site.com/hello?getVar=value" >...</a>

Solution 2

$_GET contains the keys / values that are passed to your script in the URL.

If you have the following URL :

http://www.example.com/test.php?a=10&b=plop

Then $_GET will contain :

array
  'a' => string '10' (length=2)
  'b' => string 'plop' (length=4)


Of course, as $_GET is not read-only, you could also set some values from your PHP code, if needed :

$_GET['my_value'] = 'test';

But this doesn't seem like good practice, as $_GET is supposed to contain data from the URL requested by the client.

Solution 3

If you want to fake a $_GET (or a $_POST) when including a file, you can use it like you would use any other var, like that:

$_GET['key'] = 'any get value you want';
include('your_other_file.php');

Solution 4

You can use GET variables in the action parameter of your form element. Example:

<form method="post" action="script.php?foo=bar">
    <input name="quu" ... />
    ...
</form>

This will give you foo as a GET variable and quu as a POST variable.

Solution 5

One way to set the $_GET variable is to parse the URL using parse_url() and then parse the $query string using parse_str(), which sets the variables into the $_GET global.

This approach is useful,

  • if you want to test GET parameter handling without making actual queries, e.g. for testing the incoming parameters for existence and input filtering and sanitizing of incoming vars.
  • and when you don't want to construct the array manually each time, but use the normal URL

function setGetRequest($url)
{
    $query = parse_url($url, PHP_URL_QUERY);
    parse_str($query, $_GET);
}

$url = 'http://www.example.com/test.php?a=10&b=plop';

setGetRequest($url);   

var_dump($_GET);

Result: $_GET contains

array (
  'a' => string '10' (length=2)
  'b' => string 'plop' (length=4)
)
Share:
114,876

Related videos on Youtube

dave
Author by

dave

Updated on July 09, 2022

Comments

  • dave
    dave almost 2 years

    How do i set the variable that the $_GET function will be able to use, w/o submitting a form with action = GET?

    • fabrik
      fabrik about 13 years
      I know this will be rude but i can't help: why do you want to do that?
    • dave
      dave about 13 years
      good question. basically i have links in the top php file, which is included in the index.php, but in order to know which middle page to show, i need the variable that get function will be getting to be set.
  • dave
    dave about 13 years
    but lets say how i do it w/o a link?
  • Gaurav
    Gaurav about 13 years
    probably not. $_GET works with url data or form submission (action =GET).
  • Kevin Beal
    Kevin Beal over 11 years
    If you are submitting a form to the same page you can just leave the action blank: action=""
  • Déjà vu
    Déjà vu about 9 years
    Thx, besides the "not good practice" is there any technical issue in setting $_GET? (PHP can be so surprising sometimes...)
  • Rishiraj Purohit
    Rishiraj Purohit over 8 years
    after I do this in let's say line 2 and then a javascript runs at say line 92, which asks for variable $_GET['getVar'] will the script get answer as "value". --- just FYI, script is custom google search engine javascript
  • Flash Thunder
    Flash Thunder about 8 years
    Best answer. Have no idea why the other was marked as valid.
  • Mike Q
    Mike Q over 6 years
    You can not set $_GET
  • Sagar Roy
    Sagar Roy about 6 years
    Very Good answer .. personally i like it .
  • youcantryreachingme
    youcantryreachingme over 4 years
    @MikeQ - I just did. Anyone wondering about a use case - site user registration form - on successful registration, change the parameter value from "register a new user" to "sign in" and make sure the code for signing in sits below the code for registering a new user and that you've populated any variables correctly that the login code expects. No need for page redirects.
  • Mike Q
    Mike Q over 4 years
    Well it's just a variable but the point being I see no good reason to do it. Change the URI instead and redirectr
  • oriadam
    oriadam about 4 years
    i must add that if it is using for testing/debugging it's ok, but as otherwise it is considered bad programming. it is better to pass the processed values to the function/class instead of having it read a processed $_GET. having said that i know from experience that this is not always possible, for example when including a page that you cannot edit, or an old complicated page that you don't want to mess with...
  • Vibhore Jain
    Vibhore Jain almost 3 years
    Thanks for this - $_GET['my_value'] = 'test';
  • soler
    soler over 2 years
    thanks @JosephO ! -- this tip allowed me to redirect successfully!