Get the Current URL of Page with PHP

Get the Current URL of Page with PHP

Dzhuneyt Ahmed - Author of this post

By: Dzhuneyt Ahmed

Posted · 1 min read

Outdated: This snippet hardcodes http:// and does not account for HTTPS. In modern PHP, check $_SERVER['HTTPS'] or use a framework's request abstraction to get the full URL reliably.

This simple snippet of code will help you get the full URL of the currently viewed page.

function getUrl(){ $domain = $_SERVER['HTTP_HOST']; $url = "http://" . $domain . $_SERVER['REQUEST_URI']; return $url; }

Dzhuneyt Ahmed

Dzhuneyt

Helping teams build reliable cloud infrastructure — without the bloated bill.

Social

My Other Blogs

© 2026 Dzhuneyt Ahmed. All rights reserved.