Teiv
Teiv

Reputation: 2635

Validate a query string?

first=value
first=value&arr[]=foo+bar&arr[]=baz

How can I use PHP to validate a query string, which is a sequence of

[name]=[value]&

Upvotes: 0

Views: 1469

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191789

I can't say I understand what you are asking, but if we are talking about the specific value above:

$value = $_GET['first']; //value
$arr = isset($_GET['arr']) ? $_GET['arr'] : null; //array('foo bar', 'baz')

If you are asking if you can validate whether a query string is in a usable format .. well you can't, really. There is large variety of possible query strings and you can really put in whatever you want. It is up to you to check if the values you are expecting are set and checking whether the data in them is usable for your purposes.

Upvotes: 3

Jason Fuller
Jason Fuller

Reputation: 127

<?php
if (isset($_GET['name'])) {
        $name = $_GET['name'];
        if ($name == 'value') {...};
}
if (!isset($_GET['arr'])) {
      echo "URL is not in correct format!";
      exit;
}
?>

Upvotes: 0

Related Questions