The http_build_query() will create a query string from an array or object. These strings can be appended to a URL to create a GET request, or used in a POST request with, for example, cURL.
$queryString will have the following value:
http_build_query() will also work with multi-dimensional arrays:
$queryString will have this value:
which is the URL-encoded version of
Parsing a URL
To separate a URL into its individual components, use parse_url():
After executing the above, the contents of $parts would be:
You can also selectively return just one component of the url. To return just the querystring:
Any of the following constants are accepted: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY and PHP_URL_FRAGMENT.
To further parse a query string into key value pairs use parse_str():
After execution of the above, the $params array would be populated with the following:
Redirecting to another URL
You can use the header() function to instruct the browser to redirect to a different URL:
You can also redirect to a relative URL (this is not part of the official HTTP specification, but it does work in all browsers):
If headers have been sent, you can alternatively send a meta refresh HTML tag.
WARNING: The meta refresh tag relies on HTML being properly processed by the client, and some will not do this. In general, it only works in web browsers. Also, consider that if headers have been sent, you may have a bug and this should trigger an exception.
You may also print a link for users to click, for clients that ignore the meta refresh tag:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0