Blog › PHP · Published 2026-11-26

Get the full current URL in PHP (and the proxy trap)

"What is the full URL of the current page?" PHP does not give you one variable for it; you assemble it from $_SERVER values. The official manual documents each piece, plus the proxy caveat that breaks naive code. Here is the correct assembly.

The pieces, per the manual

Assemble it

$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
    ? 'https' : 'http';

$url = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

REQUEST_URI already includes the path and the ?query part, so no separate query handling is needed.

The proxy trap

The manual's warning matters in production: behind a proxy or load balancer, $_SERVER['HTTPS'] may not report the truth. The client can connect to the load balancer over HTTPS while the balancer talks plain HTTP to your server, so your code builds http:// URLs for an https:// site. If that matches your setup, check the forwarded headers your proxy sets (commonly HTTP_X_FORWARDED_PROTO) before trusting the scheme.

Related variables worth knowing

Common mistakes

Related: PHP cURL POST example for calling other servers, and prepared statements for what goes into the database.

Official sources

Need help with your project?
We build websites, systems and automation. Start your project WhatsApp

All articles