Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP to check if a URL contains a query string

Tags:

php

This is an easy one. There seem to be plenty of solutions to determine if a URL contains a specific key or value, but strangely I can't find a solution for determining if URL does or does not have a query at all.

Using PHP, I simply want to check to see if the current URL has a query string. For example: http://abc.com/xyz/?key=value VS. http://abc.com/xyz/.

like image 334
dennisbest Avatar asked Oct 23 '11 03:10

dennisbest


People also ask

How do you check if the URL contains a given string in PHP?

Method 1: strpos() Function: The strpos() function is used to find the first occurrence of a sub string in a string. If sub string exists then the function returns the starting index of the sub string else returns False if the sub string is not found in the string (URL).

How can I tell if a URL has a query string?

To check if a url has query parameters, call the includes() method on the string, passing it a question mark as a parameter, e.g. str. includes('? ') . If the url contains query parameters, the includes method will return true , otherwise false is returned.

How can I check if a URL is working in PHP?

Existence of an URL can be checked by checking the status code in the response header. The status code 200 is Standard response for successful HTTP requests and status code 404 means URL doesn't exist. Used Functions: get_headers() Function: It fetches all the headers sent by the server in response to the HTTP request.

How do I find URL without query string?

If you want to get the last part of a URL without the query string, just use the property name window. location. pathname instead of window. location.


4 Answers

For any URL as a string:

if (parse_url($url, PHP_URL_QUERY))

http://php.net/parse_url

If it's for the URL of the current request, simply:

if ($_GET)
like image 152
deceze Avatar answered Oct 03 '22 01:10

deceze


The easiest way is probably to check to see if the $_GET[] contains anything at all. This can be done with the empty() function as follows:

if(empty($_GET)) {
    //No variables are specified in the URL.
    //Do stuff accordingly
    echo "No variables specified in URL...";
} else {
    //Variables are present. Do stuff:
    echo "Hey! Here are all the variables in the URL!\n";
    print_r($_GET);
}
like image 44
rzyns Avatar answered Oct 03 '22 01:10

rzyns


parse_url seems like the logical choice in most cases. However I can't think of a case where '?' in a URL would not denote the start of a query string so for a (very minor) performance increase you could go with

return strpos($url, '?') !== false;

Over 1,000,000 iterations the average time for strpos was about 1.6 seconds vs 1.8 for parse_url. That being said, unless your application is checking millions of URLs for query strings I'd go for parse_url.

like image 45
mickadoo Avatar answered Oct 03 '22 01:10

mickadoo


Like this:

if (isset($_SERVER['QUERY_STRING'])) {

}
like image 21
PHP Avatar answered Oct 03 '22 01:10

PHP