0

How can I retrieve the query_string, or the parameters after the '?' in the URL if it was hardcoded, and not sent through from a form?

I would normally use $_GET['name'] to retrieve this data, but since I am looking for a method to retrieve the query when someone has hardcoded by directly typing the query_string into the URL, I am unsure what the 'name' would be to use $_GET.

Is this possible?

5
  • parse_url() Commented Aug 14, 2012 at 20:08
  • 1
    That is how a GET works. Do it how you planned to. POST is the different one where you need to fetch the values from $_POST Commented Aug 14, 2012 at 20:08
  • some what confused, examples please. Commented Aug 14, 2012 at 20:09
  • I think he means that if someone's entering the query_string by hand, they might have different fieldnames than the form version. Commented Aug 14, 2012 at 20:12
  • hmm, you shouldn't really be accepting any old variable name/value through $_GET. You should be specifying what your app will accept from users. Commented Aug 14, 2012 at 20:19

2 Answers 2

1

It seems that your problem is you don't know what key the user is going to type in for the $_GET parameter. So, you can directly loop through $_GET like this:

foreach( $_GET as $key => $value) {
    echo $key . ' => ' . $value . "\n";
}

This will print all of the parameters.

Now, if you only want the first GET parameter, you can use array_shift(), like this:

$first = array_shift( $_GET);

Both methods do not require you to know the key of the parameter beforehand.

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

1 Comment

Was going to do this, but found out $queryString = $_SERVER['QUERY_STRING']; does exactly what I needed. Thanks
0
<?php
foreach($_GET as $key => $value)
    echo $key . " : " . $value;
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.