4

How can I check if an URL has parameters in it?

for instance, if the string is like this,

form_page_add.php?parent_id=1

return true

But if it is like this,

form_page_add.php?

return nothing

Thanks.

EDIT:

Sorry for not being clear, the URL is submitted from a from as a string. and I will store that string in a variable,

if(isset($_POST['cfg_path'])) $cfg_path = trim($_POST['cfg_path']);

so I need to check this variable $cfg_path whether is has parameters in it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Run
  • 54,938
  • 169
  • 450
  • 748

4 Answers4

25

Could also look for a specific key with array_key_exists(), e.g.

if(array_key_exists('some-key', $_GET))

http://php.net/manual/en/function.array-key-exists.php

barfoon
  • 27,481
  • 26
  • 92
  • 138
18

You can use this simple function:

function url_get_param($url, $name) {
    parse_str(parse_url($url, PHP_URL_QUERY), $vars);
    return isset($vars[$name]) ? $vars[$name] : null;
}

See it in action here.

It will return the value of the parameter if it exists in the url, or null if it does not appear at all. You can differentiate between a parameter having no value and not appearing at all by the identical operator (triple equals, ===).

This will work with any URL you pass it, not just $_SERVER['REQUEST_URI'].

Update:

If you just want to know if there is any parameter at all in the URL then you can use some variant of the above (see Phil's suggestion in the comments).

Or, you can use the surprisingly simple test

if (strpos($url, '=')) {
    // has at least one param
}

We don't even need to bother to check for false here, as if an equals sign exists it won't be the first character.

Update #2: While the method using strpos will work for most URLs, it's not bulletproof and so should not be used if you don't know what kind of URL you are dealing with. As Steve Onzra correctly points out in the comments, URLs like

http://example.com/2012/11/report/cGFyYW1fd2l0aF9lcXVhbA==

are valid and yet do not contain any parameter.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 1
    +1 I'd extend this to make `$name` optional. If it's omitted, return `$vars` – Phil Mar 19 '11 at 04:27
  • 1
    @Phil: I 'll leave the code as is to keep it simple, but that's a very good idea for a library function. – Jon Mar 19 '11 at 04:29
  • I was more thinking in regards to the OP's question. If you could return any and all query vars, then you can check for existence using `count(url_get_param($url))` – Phil Mar 19 '11 at 04:31
  • 3
    @Phil: Well, if you just want to know if there's *any* param in the url you can also use `strpos($url, '=')` -- low tech but efficient :) – Jon Mar 19 '11 at 04:33
  • thanks for this. but I have multiple variables sometimes so this won't work for me I think? – Run Mar 19 '11 at 04:33
  • You have to be careful with the second function since some base64 encoded data will trigger this (mainly when using frameworks): `http://example.com/2012/11/report/cGFyYW1fd2l0aF9lcXVhbA==` – Steve Tauber Dec 12 '12 at 19:49
  • @SteveOnzra: That's not a valid URL though. In order to make it valid you 'd have to encode the `=` as `%3d`, in which case there is no problem. – Jon Dec 12 '12 at 20:56
  • 1
    @Jon That would be if you want it passed as a param. It's valid per the RFC: http://stackoverflow.com/questions/1856785/characters-allowed-in-a-url – Steve Tauber Dec 12 '12 at 22:50
  • 1
    @SteveOnzra: You are so right. There's something new to learn every day! – Jon Dec 12 '12 at 23:13
  • So, would it be more efficient to do `if( strpos($url, '=') > 0 ){parse_str(parse_url($url, PHP_URL_QUERY), $vars);}`? – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ Jun 10 '14 at 08:54
  • 1
    @MatthewT.Baker: Well, the `strpos` test is not theoretically correct as discussed above. Efficiency would depend on the percentage of URLs tested that have parameters; obviously if 100% of them have then you are doing extra work for no reason. To me that looks like premature optimization and I would want to see compelling tangible evidence before checking that code in. – Jon Jun 10 '14 at 08:59
3

Another way to check would be to use parse_url() method. Check docs here. This function will return an associative array with an element 'query' which will contain the GET parameters.

Use the empty() function to check and see whether this field is empty or not. If empty, then no parameters have been passed.

Code Sample -

<?php

    $url = "http://www.sub.domain.com/index.php?key=value&key2=value2";
    print_r(parse_url($url));
?>

Output

Array
(
    [scheme] => http
    [host] => www.sub.domain.com
    [path] => /index.php
    [query] => key=value&key2=value2
)
arijeet
  • 1,858
  • 18
  • 26
0

If you are looking for whether a URL stored as a string (instead of the URL that is being called to invoke the PHP script), you can use strpos()

So you would be able to search the string for an occurence of ?, and then deal with it appropriately. For example:

$pos = strpos($myString, "?");
if($pos && $pos<strlen($myString){
    //deal with URLs with parameters
}
dmcnelis
  • 2,913
  • 1
  • 19
  • 28