6

I have the following URL

http://www.example.com/node/add/forum/3?gids[]=13

I want to get the value 13 from within my module.

I've tried with

$_GET['gid[]']

and with

$_GET['gids%5B%5D']

but I always get null.

How can I do this? Thanks

3
  • Did you tried print_r($_GET['gids']); ? or echo $_GET['gids'][1]; ? Commented May 25, 2011 at 15:20
  • … or, more generally, print_r($_REQUEST) since the OP is unsure about how that query parameter will be named. In this case, Joe has the answer below: They are automatically put into an array named 'gids'. Commented May 25, 2011 at 15:25
  • @Chumillas: I haven't. but Joe's answer put me in the right direction. Thanks. Commented May 25, 2011 at 15:29

3 Answers 3

7

Assuming the URL is correctly encoded (gids%5B%5D) and that's the only element in the array, then the contents of that first element in gids would be in $_GET['gids'][0].

Sign up to request clarification or add additional context in comments.

3 Comments

thanks: that was it!!! One more question: you write "Drupal has its own way of handling the query string", and do you know where I can read about it? I've searched but did not find documentation on this topic. Any link to useful documentation is highly appreciated.
Sorry, I was thinking about arg(), which only deals with the path query string variable ($_GET['q']). Updated to remove the bit about dealing with all $_GET variables.
If you use this, you must clean the input or you have just opened up a XSS security hole in your site.
5

Since this is tagged with the keyword "drupal" and comes up on Google searches. The Drupal 7 way of doing it and makes it easy is to use drupal_get_query_parameters(). This will return an associate array of all variables and values from the query string all at once.

By using this, when you store the information coming from the URL it has been sanitized for XSS and SQLi attacks.

1 Comment

I think this should be the accepted answer since this is the Drupal way.
3

In PHP 5.2+, use filter_input() to read GET and POST variables:

$gids = filter_input(INPUT_GET, 'gids', FILTER_DEFAULT, FILTER_FORCE_ARRAY | FILTER_FORCE_ARRAY);

1 Comment

thanks! this may be useful in the future. For my current purposes, Joe's answer did the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.