user189026
user189026

Reputation:

Check query string (PHP)

I use a query string, for example test.php?var=1.

How can I check if a user types anything after that, like another string...

I try to redirect to index.php if any other string (query string) follows my var query string.

Is it possible to check this?

For example:

test.php?var=12134  (This is a good link..)
test.php?a=23&var=123 (this is a bad link, redirect to index..)
test.php?var=123132&a=23 (this is a bad link, redirect to index..)

Upvotes: 4

Views: 27023

Answers (5)

ty812
ty812

Reputation: 3323

I think you are trying to get rid of unwanted parameters. This is usually done for security reasons.

There won't be a problem, however, if you preinitalize every variable you use and only use variables with $_GET['var'], $_POST['var'] or $_REQUEST['var'].

Upvotes: 0

Gumbo
Gumbo

Reputation: 655549

Similar to Tom Haigh’s answer, you could also get the difference of the arguments you expect and those you actually get:

$argKeys = array_keys($_GET);
$additionalArgKeys = array_diff($argKeys, array('var'));
var_dump($additionalArgKeys);

Upvotes: 3

Anax
Anax

Reputation: 9372

test.php?a=23?var=123 (this is a bad link, redirect to index..)

In this case, you only have one variable sent, named "a" containing the value "a?var=123", therefore it shouldn't be a problem for you.

test.php?var=123132&a=23 (this is a bad link, redirect to index..)

In this case you have two variables sent, ("a" and "var").

In general you can check the $_GET array to see how many variables have been sent and act accordingly, by using count($_GET).

Upvotes: 0

Tom Haigh
Tom Haigh

Reputation: 57835

I'm not sure I fully understand what you want, but if you're not interested in the positioning of the parameters this should work:

if ( isset($_GET['var']) && count($_GET) > 1 ) {
    //do something if var and another parameter is given
}

Upvotes: 23

Ollie Saunders
Ollie Saunders

Reputation: 8077

Look in $_SERVER['QUERY_STRING'].

Upvotes: 7

Related Questions