php get current url with query string – How to Get Current URL in PHP?

php get current url with query string use $_SERVER[‘QUERY_STRING’]. Get the Full URL with Query String Using $_SERVER[‘REQUEST_URI’].

use $_SERVER[‘QUERY_STRING’].

$currentURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'];

Get the Current Full URL in PHP Example Like Use http_build_url(), Use $_SERVER[], Use $_SERVER[‘HTTPS’] to Get Current Url in PHP.

php get current url with query string

You can use the $_SERVER ($_SERVER is a superglobal variable) built-in variable to get the current page URL in PHP(server scripting language).

Get entire URL, including query string and anchor

$domain = $_SERVER['HTTP_HOST'];

$path = $_SERVER['SCRIPT_NAME'];

// find out the QueryString:
$queryString = $_SERVER['QUERY_STRING'];

$connection_url = "http://" . $domain . $path . "?" . $queryString;
echo $connection_url;

$connection_url = "http://" . $domain . $_SERVER['REQUEST_URI'];
echo $connection_url;

Get the Current URL Including Parameters in PHP

$all_in_address_bar = http_build_url();

Use http_build_url() to Get Current Url in PHP

$connection_url =  http_build_url();

Use $_SERVER[] to Get Current Url in PHP

$connection_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Use $_SERVER[‘HTTPS’] to Get Current Url in PHP

$connection_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Don’t Miss : Get The Full URL

Read Also:  PHP Filter Multidimensional Array Examples

$connection_url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']

Use $_SERVER[‘HTTPS’] to check the protocol whether it is HTTP or HTTPS.

$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";

$currentURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

I hope you get an idea about php get current url with query string.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.