Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to get the current page address

Short answer

The path with parameters is in $_SERVER['REQUEST_URI']:

<?php
 
echo $_SERVER['REQUEST_URI'];

The full address

Build it from the scheme, the host and the path:

<?php
 
$scheme = ($_SERVER['HTTPS'] ?? 'off') === 'on' ? 'https' : 'http';
 
$url = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

The path only

parse_url() drops the parameters after the question mark:

<?php
 
echo parse_url('/catalog/tea?page=2&sort=price', PHP_URL_PATH);

Output:

/catalog/tea

Functions used in this recipe

Thanks for reading!

Donate to the site — we work hard for you.