How to get current full url in php?

Member

ramon

by ramon , in category: PHP , a month ago

How to get current full url in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by zita , a month ago

@ramon You can get full information about url from global array $_SERVER and here is example to get current url with domain name in php:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php

// initialize url
$url = 'http://';

// Check if https connection
if (isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS']) {
    $url = 'https://';
}

// Append the domain to the URL.
$url .= $_SERVER['HTTP_HOST'];

// Append the requested resource location to the URL
$url .= $_SERVER['REQUEST_URI'];

echo $url;