How do I get the base URL with PHP? - php

I am using XAMPP on Windows Vista. In my development, I have http://127.0.0.1/test_website/.
How do I get http://127.0.0.1/test_website/ with PHP?
I tried something like these, but none of them worked.
echo dirname(__FILE__)
or
echo basename(__FILE__);
etc.

Try this:
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
Learn more about the $_SERVER predefined variable.
If you plan on using https, you can use this:
function url(){
return sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
);
}
echo url();
#=> http://127.0.0.1/foo
Per this answer, please make sure to configure your Apache properly so you can safely depend on SERVER_NAME.
<VirtualHost *>
ServerName example.com
UseCanonicalName on
</VirtualHost>
NOTE: If you're depending on the HTTP_HOST key (which contains user input), you still have to make some cleanup, remove spaces, commas, carriage return, etc. Anything that is not a valid character for a domain. Check the PHP builtin parse_url function for an example.

Function adjusted to execute without warnings:
function url(){
if(isset($_SERVER['HTTPS'])){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
}
else{
$protocol = 'http';
}
return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

Fun 'base_url' snippet!
if (!function_exists('base_url')) {
function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){
if (isset($_SERVER['HTTP_HOST'])) {
$http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$hostname = $_SERVER['HTTP_HOST'];
$dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$core = preg_split('#/#', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
$core = $core[0];
$tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
$end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
$base_url = sprintf( $tmplt, $http, $hostname, $end );
}
else $base_url = 'http://localhost/';
if ($parse) {
$base_url = parse_url($base_url);
if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = '';
}
return $base_url;
}
}
Use as simple as:
// url like: http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php
echo base_url(); // will produce something like: http://stackoverflow.com/questions/2820723/
echo base_url(TRUE); // will produce something like: http://stackoverflow.com/
echo base_url(TRUE, TRUE); || echo base_url(NULL, TRUE); // will produce something like: http://stackoverflow.com/questions/
// and finally
echo base_url(NULL, NULL, TRUE);
// will produce something like:
// array(3) {
// ["scheme"]=>
// string(4) "http"
// ["host"]=>
// string(12) "stackoverflow.com"
// ["path"]=>
// string(35) "/questions/2820723/"
// }

$base_url="http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["REQUEST_URI"].'?').'/';
Usage:
print "<script src='{$base_url}js/jquery.min.js'/>";

$modifyUrl = parse_url($url);
print_r($modifyUrl)
Its just simple to use
Output :
Array
(
[scheme] => http
[host] => aaa.bbb.com
[path] => /
)

I think the $_SERVER superglobal has the information you're looking for. It might be something like this:
echo $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
You can see the relevant PHP documentation here.

Try the following code :
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
echo $config['base_url'];
The first line is checking if your base url used http or https then the second line is use to get the hostname .Then the third line use to get only base folder of your site ex. /test_website/

The following code will reduce the problem to check the protocol.
The $_SERVER['APP_URL'] will display the domain name with the protocol
$_SERVER['APP_URL'] will return protocol://domain ( eg:-http://localhost)
$_SERVER['REQUEST_URI'] for remaining parts of the url such as /directory/subdirectory/something/else
$url = $_SERVER['APP_URL'].$_SERVER['REQUEST_URI'];
The output would be like this
http://localhost/directory/subdirectory/something/else

Simple and easy trick:
$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$baseurl = "http://" . $host . $path . "/";
URL looks like this: http://example.com/folder/

You can do it like this, but sorry my english is not good enough.
First, get home base url with this simple code..
I've tested this code on my local server and public and the result is good.
<?php
function home_base_url(){
// first get http protocol if http or https
$base_url = (isset($_SERVER['HTTPS']) &&
$_SERVER['HTTPS']!='off') ? 'https://' : 'http://';
// get default website root directory
$tmpURL = dirname(__FILE__);
// when use dirname(__FILE__) will return value like this "C:\xampp\htdocs\my_website",
//convert value to http url use string replace,
// replace any backslashes to slash in this case use chr value "92"
$tmpURL = str_replace(chr(92),'/',$tmpURL);
// now replace any same string in $tmpURL value to null or ''
// and will return value like /localhost/my_website/ or just /my_website/
$tmpURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$tmpURL);
// delete any slash character in first and last of value
$tmpURL = ltrim($tmpURL,'/');
$tmpURL = rtrim($tmpURL, '/');
// check again if we find any slash string in value then we can assume its local machine
if (strpos($tmpURL,'/')){
// explode that value and take only first value
$tmpURL = explode('/',$tmpURL);
$tmpURL = $tmpURL[0];
}
// now last steps
// assign protocol in first value
if ($tmpURL !== $_SERVER['HTTP_HOST'])
// if protocol its http then like this
$base_url .= $_SERVER['HTTP_HOST'].'/'.$tmpURL.'/';
else
// else if protocol is https
$base_url .= $tmpURL.'/';
// give return value
return $base_url;
}
?>
// and test it
echo home_base_url();
output will like this :
local machine : http://localhost/my_website/ or https://myhost/my_website
public : http://www.my_website.com/ or https://www.my_website.com/
use home_base_url function at index.php of your website and define it
and then you can use this function to load scripts, css and content via url like
<?php
echo '<script type="text/javascript" src="'.home_base_url().'js/script.js"></script>'."\n";
?>
will create output like this :
<script type="text/javascript" src="http://www.my_website.com/js/script.js"></script>
and if this script works fine,,!

I found this on
http://webcheatsheet.com/php/get_current_page_url.php
Add the following code to a page:
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
You can now get the current page URL using the line:
<?php
echo curPageURL();
?>
Sometimes it is needed to get the page name only. The following example shows how to do it:
<?php
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
echo "The current page name is ".curPageName();
?>

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

Try this. It works for me.
/*url.php file*/
trait URL {
private $url = '';
private $current_url = '';
public $get = '';
function __construct()
{
$this->url = $_SERVER['SERVER_NAME'];
$this->current_url = $_SERVER['REQUEST_URI'];
$clean_server = str_replace('', $this->url, $this->current_url);
$clean_server = explode('/', $clean_server);
$this->get = array('base_url' => "/".$clean_server[1]);
}
}
Use like this:
<?php
/*
Test file
Tested for links:
http://localhost/index.php
http://localhost/
http://localhost/index.php/
http://localhost/url/index.php
http://localhost/url/index.php/
http://localhost/url/ab
http://localhost/url/ab/c
*/
require_once 'sys/url.php';
class Home
{
use URL;
}
$h = new Home();
?>
Base

This is the best method i think so.
$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") ? "https" : "http");
$base_url .= "://".$_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
echo $base_url;

The following solution will work even when the current url has request query string.
<?php
function baseUrl($file=__FILE__){
$currentFile = array_reverse(explode(DIRECTORY_SEPARATOR,$file))[0];
if(!empty($_SERVER['QUERY_STRING'])){
$currentFile.='?'.$_SERVER['QUERY_STRING'];
}
$protocol = $_SERVER['PROTOCOL'] == isset($_SERVER['HTTPS']) &&
!empty($_SERVER['HTTPS']) ? 'https' : 'http';
$url = "$protocol://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = str_replace($currentFile, '', $url);
return $url;
}
The calling file will provide the __FILE__ as param
<?= baseUrl(__FILE__)?>

Here's one I just put together that works for me. It will return an array with 2 elements. The first element is everything before the ? and the second is an array containing all of the query string variables in an associative array.
function disectURL()
{
$arr = array();
$a = explode('?',sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
));
$arr['base_url'] = $a[0];
$arr['query_string'] = [];
if(sizeof($a) == 2)
{
$b = explode('&', $a[1]);
$qs = array();
foreach ($b as $c)
{
$d = explode('=', $c);
$qs[$d[0]] = $d[1];
}
$arr['query_string'] = (count($qs)) ? $qs : '';
}
return $arr;
}
Note: This is an expansion of the answer provided by maček above. (Credit where credit is due.)

Edited at #user3832931 's answer to include server port..
to form URLs like 'https://localhost:8000/folder/'
$base_url="http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].dirname($_SERVER["REQUEST_URI"].'?').'/';

Try using: $_SERVER['SERVER_NAME'];
I used it to echo the base url of my site to link my css.
<link href="//<?php echo $_SERVER['SERVER_NAME']; ?>/assets/css/your-stylesheet.css" rel="stylesheet" type="text/css">
Hope this helps!

$some_variable = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['REQUEST_URI'], "/")+1);
and you get
something like
lalala/tralala/something/

In my case I needed the base URL similar to the RewriteBasecontained in the .htaccess file.
Unfortunately simply retrieving the RewriteBase from the .htaccess file is impossible with PHP. But it is possible to set an environment variable in the .htaccess file and then retrieve that variable in PHP. Just check these bits of code out:
.htaccess
SetEnv BASE_PATH /
index.php
Now I use this in the base tag of the template (in the head section of the page):
<base href="<?php echo ! empty( getenv( 'BASE_PATH' ) ) ? getenv( 'BASE_PATH' ) : '/'; ?>"/>
So if the variable was not empty, we use it. Otherwise fallback to / as default base path.
Based on the environment the base url will always be correct. I use / as the base url on local and production websites. But /foldername/ for on the staging environment.
They all had their own .htaccess in the first place because the RewriteBase was different. So this solution works for me.

Currently this is the proper answer:
$baseUrl = $_SERVER['REQUEST_SCHEME'];
$baseUrl .= '://'.$_SERVER['HTTP_HOST'];
You might want to add a / to the end. Or you might want to add
$baseUrl .= $_SERVER['REQUEST_URI'];
so in total copy paste
$baseUrl = $_SERVER['REQUEST_SCHEME']
. '://' . $_SERVER['HTTP_HOST']
. $_SERVER['REQUEST_URI'];

function server_url(){
$server ="";
if(isset($_SERVER['SERVER_NAME'])){
$server = sprintf("%s://%s%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME'], '/');
}
else{
$server = sprintf("%s://%s%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_ADDR'], '/');
}
print $server;
}

I had the same question as the OP, but maybe a different requirement. I created this function...
/**
* Get the base URL of the current page. For example, if the current page URL is
* "https://example.com/dir/example.php?whatever" this function will return
* "https://example.com/dir/" .
*
* #return string The base URL of the current page.
*/
function get_base_url() {
$protocol = filter_input(INPUT_SERVER, 'HTTPS');
if (empty($protocol)) {
$protocol = "http";
}
$host = filter_input(INPUT_SERVER, 'HTTP_HOST');
$request_uri_full = filter_input(INPUT_SERVER, 'REQUEST_URI');
$last_slash_pos = strrpos($request_uri_full, "/");
if ($last_slash_pos === FALSE) {
$request_uri_sub = $request_uri_full;
}
else {
$request_uri_sub = substr($request_uri_full, 0, $last_slash_pos + 1);
}
return $protocol . "://" . $host . $request_uri_sub;
}
...which, incidentally, I use to help create absolute URLs that should be used for redirecting.

Just test and get the result.
// output: /myproject/index.php
$currentPath = $_SERVER['PHP_SELF'];
// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
$pathInfo = pathinfo($currentPath);
// output: localhost
$hostName = $_SERVER['HTTP_HOST'];
// output: http://
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
// return: http://localhost/myproject/
echo $protocol.$hostName.$pathInfo['dirname']."/";

$http = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'? "https://" : "http://";
$dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '',$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']);
echo $url = $http . $dir;
// echo $url = $http . $_SERVER["SERVER_NAME"] . $_SERVER['REQUEST_URI'];

Related

Web Development | 404 HTML Coding [duplicate]

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
I use this code to get the full URL:
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
The problem is that I use some masks in my .htaccess, so what we see in the URL is not always the real path of the file.
What I need is to get the URL, what is written in the URL, nothing more and nothing less—the full URL.
I need to get how it appears in the Navigation Bar in the web browser, and not the real path of the file on the server.
Have a look at $_SERVER['REQUEST_URI'], i.e.
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
(Note that the double quoted string syntax is perfectly correct)
If you want to support both HTTP and HTTPS, you can use
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.
Short version to output link on a webpage
$url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
Here are some more details about the issues and edge cases of the //example.com/path/ format
Full version
function url_origin( $s, $use_forwarded_host = false )
{
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url( $s, $use_forwarded_host = false )
{
return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}
$absolute_url = full_url( $_SERVER );
echo $absolute_url;
This is a heavily modified version of http://snipplr.com/view.php?codeview&id=2734 (Which no longer exists)
URL structure:
scheme://username:password#domain:port/path?query_string#fragment_id
The parts in bold will not be included by the function
Notes:
This function does not include username:password from a full URL or the fragment (hash).
It will not show the default port 80 for HTTP and port 443 for HTTPS.
Only tested with http and https schemes.
The #fragment_id is not sent to the server by the client (browser) and will not be added to the full URL.
$_GET will only contain foo=bar2 for an URL like /example?foo=bar1&foo=bar2.
Some CMS's and environments will rewrite $_SERVER['REQUEST_URI'] and return /example?foo=bar2 for an URL like /example?foo=bar1&foo=bar2, use $_SERVER['QUERY_STRING'] in this case.
Keep in mind that an URI = URL + URN, but due to popular use, URL now means both URI and URL.
You should remove HTTP_X_FORWARDED_HOST if you do not plan to use proxies or balancers.
The spec says that the Host header must contain the port number unless it is the default number.
Client (Browser) controlled variables:
$_SERVER['REQUEST_URI']. Any unsupported characters are encoded by the browser before they are sent.
$_SERVER['HTTP_HOST'] and is not always available according to comments in the PHP manual: http://php.net/manual/en/reserved.variables.php
$_SERVER['HTTP_X_FORWARDED_HOST'] gets set by balancers and is not mentioned in the list of $_SERVER variables in the PHP manual.
Server controlled variables:
$_SERVER['HTTPS']. The client chooses to use this, but the server returns the actual value of either empty or "on".
$_SERVER['SERVER_PORT']. The server only accepts allowed numbers as ports.
$_SERVER['SERVER_PROTOCOL']. The server only accepts certain protocols.
$_SERVER['SERVER_NAME'] . It is set manually in the server configuration and is not available for IPv6 according to kralyk.
Related:
What is the difference between HTTP_HOST and SERVER_NAME in PHP?
Is Port Number Required in HTTP "Host" Header Parameter?
https://stackoverflow.com/a/28049503/175071
Examples for: https://(www.)example.com/subFolder/myfile.php?var=blabla#555
// ======= PATHINFO ====== //
$x = pathinfo($url);
$x['dirname'] 🡺 https://example.com/subFolder
$x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure!
$x['extension'] 🡺 php?var=blabla#555 // Unsecure!
$x['filename'] 🡺 myfile
// ======= PARSE_URL ====== //
$x = parse_url($url);
$x['scheme'] 🡺 https
$x['host'] 🡺 example.com
$x['path'] 🡺 /subFolder/myfile.php
$x['query'] 🡺 var=blabla
$x['fragment'] 🡺 555
//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"] 🡺 143.34.112.23
$_SERVER["SERVER_PORT"] 🡺 80 (or 443, etc..)
$_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST'] 🡺 example.com (or with WWW) //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"] 🡺 var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
__DIR__ 🡺 /home/user/public_html/subFolder //same: dirname(__FILE__)
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺 /subFolder/myfile.php
$_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php
// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"] 🡺 /parentfile.php
$_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses HTTPS:
$HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' ); //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO']) ...
// To trim values to filename, i.e.
basename($url) 🡺 myfile.php
// Excellent solution to find origin
$debug_files = debug_backtrace();
$caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
Notice!
The hashtag # parts were manually used in the above example just for illustration purposes, however, server-side languages (including PHP) can't natively detect them (only JavaScript can do that, as the hashtag is only browser/client side functionality).
DIRECTORY_SEPARATOR returns \ for Windows-type hosting, instead of /.
____
For WordPress
// (Let's say, if WordPress is installed in subdirectory: http://example.com/wpdir/)
home_url() 🡺 http://example.com/wpdir/ // If is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri() 🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo('template_url') ]
get_stylesheet_directory() 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__) 🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__) 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
Here's a solution using a ternary statement, keeping the code minimal:
$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s" : "") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
This is the smallest and easiest way to do this, assuming one's web server is using the standard port 443 for HTTPS.
My favorite cross platform method for finding the current URL is:
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Simply use:
$uri = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
function full_path()
{
$s = &$_SERVER;
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
$host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
$uri = $protocol . '://' . $host . $s['REQUEST_URI'];
$segments = explode('?', $uri, 2);
$url = $segments[0];
return $url;
}
Note: I just made an update to Timo Huovinen's code, so you won't get any GET parameters in the URL. This URL is plain and removes things like ?hi=i&am=a&get.
Example:
http://www.example.com/index?get=information
will be shown as:
http://www.example.com/index
This is fine unless you use GET paramaters to define some specific content, in which case you should use his code! :-)
Clear code, working in all webservers (Apache, nginx, IIS, ...):
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Same technique as the accepted answer, but with HTTPS support, and more readable:
$current_url = sprintf(
'%s://%s/%s',
isset($_SERVER['HTTPS']) ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
);
The above gives unwanted slashes. On my setup Request_URI has leading and trailing slashes. This works better for me.
$Current_Url = sprintf(
'%s://%s/%s',
isset($_SERVER['HTTPS']) ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
trim($_SERVER['REQUEST_URI'],'/\\')
);
HTTP_HOST and REQUEST_URI must be in quotes, otherwise it throws an error in PHP 7.2
Use:
$actual_link = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
If you want to support both HTTP and HTTPS:
$actual_link = (isset($_SERVER['HTTPS']) ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Here is my solution - code is inspired by Tracy Debugger. It was changed for supporting different server ports. You can get full current URL including $_SERVER['REQUEST_URI'] or just the basic server URL. Check my function:
function getCurrentUrl($full = true) {
if (isset($_SERVER['REQUEST_URI'])) {
$parse = parse_url(
(isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') .
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '')) . (($full) ? $_SERVER['REQUEST_URI'] : null)
);
$parse['port'] = $_SERVER["SERVER_PORT"]; // Setup protocol for sure (80 is default)
return http_build_url('', $parse);
}
}
Here is test code:
// Follow $_SERVER variables was set only for test
$_SERVER['HTTPS'] = 'off'; // on
$_SERVER['SERVER_PORT'] = '9999'; // Setup
$_SERVER['HTTP_HOST'] = 'some.crazy.server.5.name:8088'; // Port is optional there
$_SERVER['REQUEST_URI'] = '/150/tail/single/normal?get=param';
echo getCurrentUrl();
// http://some.crazy.server.5.name:9999/150/tail/single/normal?get=param
echo getCurrentUrl(false);
// http://some.crazy.server.5.name:9999/
I've made this function to handle the URL:
<?php
function curPageURL()
{
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .=
$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
This is quite easy to do with your Apache environment variables. This only works with Apache 2, which I assume you are using.
Simply use the following PHP code:
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
echo $request_url;
?>
This is the solution for your problem:
//Fetch page URL by this
$url = $_SERVER['REQUEST_URI'];
echo "$url<br />";
//It will print
//fetch host by this
$host=$_SERVER['HTTP_HOST'];
echo "$host<br />";
//You can fetch the full URL by this
$fullurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $fullurl;
Try this:
print_r($_SERVER);
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.
$HTTP_SERVER_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_SERVER_VARS and $_SERVER are different variables and that PHP handles them as such)
You can use http_build_url with no arguments to get the full URL of the current page:
$url = http_build_url();
Use this one-liner to find the parent folder URL (if you have no access to http_build_url() that comes along with pecl_http):
$url = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['SERVER_NAME'].str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname(dirname(__FILE__)));
Here is the basis of a more secure version of the accepted answer, using PHP's filter_input function, which also makes up for the potential lack of $_SERVER['REQUEST_URI']:
$protocol_https = filter_input(INPUT_SERVER, 'HTTPS', FILTER_SANITIZE_STRING);
$host = filter_input(INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL);
$request_uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL);
if(strlen($request_uri) == 0)
{
$request_uri = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_URL);
$query_string = filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_URL);
if($query_string)
{
$request_uri .= '?' . $query_string;
}
}
$full_url = ($protocol_https ? 'https' : 'http') . '://' . $host . $request_uri;
You could use some different filters to tweak it to your liking.
public static function getCurrentUrl($withQuery = true)
{
$protocol = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
or (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https')
or (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
or (isset($_SERVER['SERVER_PORT']) && intval($_SERVER['SERVER_PORT']) === 443) ? 'https' : 'http';
$uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $withQuery ? $uri : str_replace('?' . $_SERVER['QUERY_STRING'], '', $uri);
}
I have used the below code, and it is working fine for me, for both cases, HTTP and HTTPS.
function curPageURL() {
if(isset($_SERVER["HTTPS"]) && !empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] != 'on' )) {
$url = 'https://'.$_SERVER["SERVER_NAME"];//https url
} else {
$url = 'http://'.$_SERVER["SERVER_NAME"];//http url
}
if(( $_SERVER["SERVER_PORT"] != 80 )) {
$url .= $_SERVER["SERVER_PORT"];
}
$url .= $_SERVER["REQUEST_URI"];
return $url;
}
echo curPageURL();
Demo
I used this statement.
$base = "http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]$my_web_base_path";
$url = $base . "/" . dirname(dirname(__FILE__));
Very simple use:
function current_url() {
$current_url = ( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$current_url .= ( $_SERVER["SERVER_PORT"] != 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$current_url .= $_SERVER["REQUEST_URI"];
return $current_url;
}
Use:
$base_dir = __DIR__; // Absolute path to your installation, ex: /var/www/mywebsite
$doc_root = preg_replace("!{$_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']); # ex: /var/www
$base_url = preg_replace("!^{$doc_root}!", '', $base_dir); # ex: '' or '/mywebsite'
$base_url = str_replace('\\', '/', $base_url);//On Windows
$base_url = str_replace($doc_root, '', $base_url);//On Windows
$protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$port = $_SERVER['SERVER_PORT'];
$disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port";
$domain = $_SERVER['SERVER_NAME'];
$full_url = "$protocol://{$domain}{$disp_port}{$base_url}"; # Ex: 'http://example.com', 'https://example.com/mywebsite', etc.
Source: PHP Document Root, Path and URL detection
This works for both HTTP and HTTPS.
echo 'http' . (($_SERVER['HTTPS'] == 'on') ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Output something like this.
https://example.com/user.php?token=3f0d9sickc0flmg8hnsngk5u07&access_level=application
You can make use of HTTP_ORIGIN as illustrated in the snippet below:
if ( ! array_key_exists( 'HTTP_ORIGIN', $_SERVER ) ) {
$this->referer = $_SERVER['SERVER_NAME'];
} else {
$this->referer = $_SERVER['HTTP_ORIGIN'];
}
I think this method is good..try it
if($_SERVER['HTTP_HOST'] == "localhost"){
define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('SITEPATH', $_SERVER['DOCUMENT_ROOT']);
define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/');
define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/');
}
else{
define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('SITEPATH', $_SERVER['DOCUMENT_ROOT']);
define('TEMPLATE', $_SERVER['DOCUMENT_ROOT'] . '/incs/template/');
define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/');
define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/');
}
$page_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
For more: How to get the full URL of a page using PHP

PHP how to get the base domain/url?

function url(){
if(isset($_SERVER['HTTPS'])){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
}
else{
$protocol = 'http';
}
return $protocol . "://" . $_SERVER['HTTP_HOST'];
}
For example with the function above, it works fine if I work with the same directory, but if I make a sub directory, and work in it, it will give me the location of the sub directory also for example. I just want example.com but it gives me example.com/sub if I'm working in the folder sub. If I'm using the main directory,the function works fine. Is there an alternative to $_SERVER['HTTP_HOST']?
Or how could I fix my function/code to get the main url only? Thanks.
Use SERVER_NAME.
echo $_SERVER['SERVER_NAME']; //Outputs www.example.com
You could use PHP's parse_url() function
function url($url) {
$result = parse_url($url);
return $result['scheme']."://".$result['host'];
}
Shortest solution:
$domain = parse_url('http://google.com', PHP_URL_HOST);
/**
* Suppose, you are browsing in your localhost
* http://localhost/myproject/index.php?id=8
*/
function getBaseUrl()
{
// output: /myproject/index.php
$currentPath = $_SERVER['PHP_SELF'];
// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
$pathInfo = pathinfo($currentPath);
// output: localhost
$hostName = $_SERVER['HTTP_HOST'];
// output: http://
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';
// return: http://localhost/myproject/
return $protocol.'://'.$hostName.$pathInfo['dirname']."/";
}
Use parse_url() like this:
function url(){
if(isset($_SERVER['HTTPS'])){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
}
else{
$protocol = 'http';
}
return $protocol . "://" . parse_url($_SERVER['REQUEST_URI'], PHP_URL_HOST);
}
Here is another shorter option:
function url(){
$pu = parse_url($_SERVER['REQUEST_URI']);
return $pu["scheme"] . "://" . $pu["host"];
}
Step-1
First trim the trailing backslash (/) from the URL. For example, If the URL is http://www.google.com/ then the resultant URL will be http://www.google.com
$url= trim($url, '/');
Step-2
If scheme not included in the URL, then prepend it. So for example if the URL is www.google.com then the resultant URL will be http://www.google.com
if (!preg_match('#^http(s)?://#', $url)) {
$url = 'http://' . $url;
}
Step-3
Get the parts of the URL.
$urlParts = parse_url($url);
Step-4
Now remove www. from the URL
$domain = preg_replace('/^www\./', '', $urlParts['host']);
Your final domain without http and www is now stored in $domain variable.
Examples:
http://www.google.com => google.com
https://www.google.com => google.com
www.google.com => google.com
http://google.com => google.com
2 lines to solve it
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$myDomain = preg_replace('/^www\./', '', parse_url($actual_link, PHP_URL_HOST));
/* Get sub domain or main domain url
* $url is $_SERVER['SERVER_NAME']
* $index int remove subdomain if acceess from sub domain my current url is https://support.abcd.com ("support" = 7 (char))
* $subDomain string
* $issecure string https or http
* return url
* call like echo getUrl($_SERVER['SERVER_NAME'],7,"payment",true,false);
* out put https://payment.abcd.com
* second call echo getUrl($_SERVER['SERVER_NAME'],7,null,true,true);
*/
function getUrl($url,$index,$subDomain=null,$issecure=false,$www=true) {
//$url=$_SERVER['SERVER_NAME']
$protocol=($issecure==true) ? "https://" : "http://";
$url= substr($url,$index);
$www =($www==true) ? "www": "";
$url= empty($subDomain) ? $protocol.$url :
$protocol.$www.$subDomain.$url;
return $url;
}
Use this code is whork :
if (!preg_match('#^http(s)?://#', $url)) {
$url = 'http://' . $url;
}
$urlParts = parse_url($url);
$url = preg_replace('/^www\./', '', $urlParts['host']);
This works fine if you want the http protocol also since it could be http or https.
$domainURL = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'];
Please try this:
$uri = $_SERVER['REQUEST_URI']; // $uri == example.com/sub
$exploded_uri = explode('/', $uri); //$exploded_uri == array('example.com','sub')
$domain_name = $exploded_uri[1]; //$domain_name = 'example.com'
I hope this will help you.
Tenary Operator helps keep it short and simple.
echo (isset($_SERVER['HTTPS']) ? 'http' : 'https' ). "://" . $_SERVER['SERVER_NAME'] ;
If you're using wordpress, use get_site_url:
get_site_url()

Obtain full URL address

I want obtain full URL adres in php script, tell please, this code always give correct URL?
or maybe in some case, this code give us wrong URL ?
if (empty($_SERVER['HTTPS'])) {
$s = "";
}
else {
$s = "s";
}
$url = "http".$s."://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
You forget port number. This is full function code to receive current page URL.
function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
I think it should be fine assuming you are using standard port 80, otherwise you might want to include that as well.
$uri = 'http'. ($_SERVER['HTTPS'] ? 's' : null) .'://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Get the full URL in PHP

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
I use this code to get the full URL:
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
The problem is that I use some masks in my .htaccess, so what we see in the URL is not always the real path of the file.
What I need is to get the URL, what is written in the URL, nothing more and nothing less—the full URL.
I need to get how it appears in the Navigation Bar in the web browser, and not the real path of the file on the server.
Have a look at $_SERVER['REQUEST_URI'], i.e.
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
(Note that the double quoted string syntax is perfectly correct)
If you want to support both HTTP and HTTPS, you can use
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.
Short version to output link on a webpage
$url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
Here are some more details about the issues and edge cases of the //example.com/path/ format
Full version
function url_origin( $s, $use_forwarded_host = false )
{
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url( $s, $use_forwarded_host = false )
{
return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}
$absolute_url = full_url( $_SERVER );
echo $absolute_url;
This is a heavily modified version of http://snipplr.com/view.php?codeview&id=2734 (Which no longer exists)
URL structure:
scheme://username:password#domain:port/path?query_string#fragment_id
The parts in bold will not be included by the function
Notes:
This function does not include username:password from a full URL or the fragment (hash).
It will not show the default port 80 for HTTP and port 443 for HTTPS.
Only tested with http and https schemes.
The #fragment_id is not sent to the server by the client (browser) and will not be added to the full URL.
$_GET will only contain foo=bar2 for an URL like /example?foo=bar1&foo=bar2.
Some CMS's and environments will rewrite $_SERVER['REQUEST_URI'] and return /example?foo=bar2 for an URL like /example?foo=bar1&foo=bar2, use $_SERVER['QUERY_STRING'] in this case.
Keep in mind that an URI = URL + URN, but due to popular use, URL now means both URI and URL.
You should remove HTTP_X_FORWARDED_HOST if you do not plan to use proxies or balancers.
The spec says that the Host header must contain the port number unless it is the default number.
Client (Browser) controlled variables:
$_SERVER['REQUEST_URI']. Any unsupported characters are encoded by the browser before they are sent.
$_SERVER['HTTP_HOST'] and is not always available according to comments in the PHP manual: http://php.net/manual/en/reserved.variables.php
$_SERVER['HTTP_X_FORWARDED_HOST'] gets set by balancers and is not mentioned in the list of $_SERVER variables in the PHP manual.
Server controlled variables:
$_SERVER['HTTPS']. The client chooses to use this, but the server returns the actual value of either empty or "on".
$_SERVER['SERVER_PORT']. The server only accepts allowed numbers as ports.
$_SERVER['SERVER_PROTOCOL']. The server only accepts certain protocols.
$_SERVER['SERVER_NAME'] . It is set manually in the server configuration and is not available for IPv6 according to kralyk.
Related:
What is the difference between HTTP_HOST and SERVER_NAME in PHP?
Is Port Number Required in HTTP "Host" Header Parameter?
https://stackoverflow.com/a/28049503/175071
Examples for: https://(www.)example.com/subFolder/myfile.php?var=blabla#555
// ======= PATHINFO ====== //
$x = pathinfo($url);
$x['dirname'] 🡺 https://example.com/subFolder
$x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure!
$x['extension'] 🡺 php?var=blabla#555 // Unsecure!
$x['filename'] 🡺 myfile
// ======= PARSE_URL ====== //
$x = parse_url($url);
$x['scheme'] 🡺 https
$x['host'] 🡺 example.com
$x['path'] 🡺 /subFolder/myfile.php
$x['query'] 🡺 var=blabla
$x['fragment'] 🡺 555
//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"] 🡺 143.34.112.23
$_SERVER["SERVER_PORT"] 🡺 80 (or 443, etc..)
$_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST'] 🡺 example.com (or with WWW) //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"] 🡺 var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
__DIR__ 🡺 /home/user/public_html/subFolder //same: dirname(__FILE__)
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺 /subFolder/myfile.php
$_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php
// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"] 🡺 /parentfile.php
$_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses HTTPS:
$HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' ); //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO']) ...
// To trim values to filename, i.e.
basename($url) 🡺 myfile.php
// Excellent solution to find origin
$debug_files = debug_backtrace();
$caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
Notice!
The hashtag # parts were manually used in the above example just for illustration purposes, however, server-side languages (including PHP) can't natively detect them (only JavaScript can do that, as the hashtag is only browser/client side functionality).
DIRECTORY_SEPARATOR returns \ for Windows-type hosting, instead of /.
____
For WordPress
// (Let's say, if WordPress is installed in subdirectory: http://example.com/wpdir/)
home_url() 🡺 http://example.com/wpdir/ // If is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri() 🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo('template_url') ]
get_stylesheet_directory() 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__) 🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__) 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
Here's a solution using a ternary statement, keeping the code minimal:
$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s" : "") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
This is the smallest and easiest way to do this, assuming one's web server is using the standard port 443 for HTTPS.
My favorite cross platform method for finding the current URL is:
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Simply use:
$uri = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
function full_path()
{
$s = &$_SERVER;
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
$host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
$uri = $protocol . '://' . $host . $s['REQUEST_URI'];
$segments = explode('?', $uri, 2);
$url = $segments[0];
return $url;
}
Note: I just made an update to Timo Huovinen's code, so you won't get any GET parameters in the URL. This URL is plain and removes things like ?hi=i&am=a&get.
Example:
http://www.example.com/index?get=information
will be shown as:
http://www.example.com/index
This is fine unless you use GET paramaters to define some specific content, in which case you should use his code! :-)
Clear code, working in all webservers (Apache, nginx, IIS, ...):
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Same technique as the accepted answer, but with HTTPS support, and more readable:
$current_url = sprintf(
'%s://%s/%s',
isset($_SERVER['HTTPS']) ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
);
The above gives unwanted slashes. On my setup Request_URI has leading and trailing slashes. This works better for me.
$Current_Url = sprintf(
'%s://%s/%s',
isset($_SERVER['HTTPS']) ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
trim($_SERVER['REQUEST_URI'],'/\\')
);
HTTP_HOST and REQUEST_URI must be in quotes, otherwise it throws an error in PHP 7.2
Use:
$actual_link = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
If you want to support both HTTP and HTTPS:
$actual_link = (isset($_SERVER['HTTPS']) ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Here is my solution - code is inspired by Tracy Debugger. It was changed for supporting different server ports. You can get full current URL including $_SERVER['REQUEST_URI'] or just the basic server URL. Check my function:
function getCurrentUrl($full = true) {
if (isset($_SERVER['REQUEST_URI'])) {
$parse = parse_url(
(isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') .
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '')) . (($full) ? $_SERVER['REQUEST_URI'] : null)
);
$parse['port'] = $_SERVER["SERVER_PORT"]; // Setup protocol for sure (80 is default)
return http_build_url('', $parse);
}
}
Here is test code:
// Follow $_SERVER variables was set only for test
$_SERVER['HTTPS'] = 'off'; // on
$_SERVER['SERVER_PORT'] = '9999'; // Setup
$_SERVER['HTTP_HOST'] = 'some.crazy.server.5.name:8088'; // Port is optional there
$_SERVER['REQUEST_URI'] = '/150/tail/single/normal?get=param';
echo getCurrentUrl();
// http://some.crazy.server.5.name:9999/150/tail/single/normal?get=param
echo getCurrentUrl(false);
// http://some.crazy.server.5.name:9999/
I've made this function to handle the URL:
<?php
function curPageURL()
{
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .=
$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
This is quite easy to do with your Apache environment variables. This only works with Apache 2, which I assume you are using.
Simply use the following PHP code:
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
echo $request_url;
?>
This is the solution for your problem:
//Fetch page URL by this
$url = $_SERVER['REQUEST_URI'];
echo "$url<br />";
//It will print
//fetch host by this
$host=$_SERVER['HTTP_HOST'];
echo "$host<br />";
//You can fetch the full URL by this
$fullurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $fullurl;
Try this:
print_r($_SERVER);
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.
$HTTP_SERVER_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_SERVER_VARS and $_SERVER are different variables and that PHP handles them as such)
You can use http_build_url with no arguments to get the full URL of the current page:
$url = http_build_url();
Use this one-liner to find the parent folder URL (if you have no access to http_build_url() that comes along with pecl_http):
$url = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['SERVER_NAME'].str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname(dirname(__FILE__)));
Here is the basis of a more secure version of the accepted answer, using PHP's filter_input function, which also makes up for the potential lack of $_SERVER['REQUEST_URI']:
$protocol_https = filter_input(INPUT_SERVER, 'HTTPS', FILTER_SANITIZE_STRING);
$host = filter_input(INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL);
$request_uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL);
if(strlen($request_uri) == 0)
{
$request_uri = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_URL);
$query_string = filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_URL);
if($query_string)
{
$request_uri .= '?' . $query_string;
}
}
$full_url = ($protocol_https ? 'https' : 'http') . '://' . $host . $request_uri;
You could use some different filters to tweak it to your liking.
public static function getCurrentUrl($withQuery = true)
{
$protocol = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
or (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https')
or (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
or (isset($_SERVER['SERVER_PORT']) && intval($_SERVER['SERVER_PORT']) === 443) ? 'https' : 'http';
$uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $withQuery ? $uri : str_replace('?' . $_SERVER['QUERY_STRING'], '', $uri);
}
I have used the below code, and it is working fine for me, for both cases, HTTP and HTTPS.
function curPageURL() {
if(isset($_SERVER["HTTPS"]) && !empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] != 'on' )) {
$url = 'https://'.$_SERVER["SERVER_NAME"];//https url
} else {
$url = 'http://'.$_SERVER["SERVER_NAME"];//http url
}
if(( $_SERVER["SERVER_PORT"] != 80 )) {
$url .= $_SERVER["SERVER_PORT"];
}
$url .= $_SERVER["REQUEST_URI"];
return $url;
}
echo curPageURL();
Demo
I used this statement.
$base = "http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]$my_web_base_path";
$url = $base . "/" . dirname(dirname(__FILE__));
Very simple use:
function current_url() {
$current_url = ( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$current_url .= ( $_SERVER["SERVER_PORT"] != 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$current_url .= $_SERVER["REQUEST_URI"];
return $current_url;
}
Use:
$base_dir = __DIR__; // Absolute path to your installation, ex: /var/www/mywebsite
$doc_root = preg_replace("!{$_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']); # ex: /var/www
$base_url = preg_replace("!^{$doc_root}!", '', $base_dir); # ex: '' or '/mywebsite'
$base_url = str_replace('\\', '/', $base_url);//On Windows
$base_url = str_replace($doc_root, '', $base_url);//On Windows
$protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$port = $_SERVER['SERVER_PORT'];
$disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port";
$domain = $_SERVER['SERVER_NAME'];
$full_url = "$protocol://{$domain}{$disp_port}{$base_url}"; # Ex: 'http://example.com', 'https://example.com/mywebsite', etc.
Source: PHP Document Root, Path and URL detection
This works for both HTTP and HTTPS.
echo 'http' . (($_SERVER['HTTPS'] == 'on') ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Output something like this.
https://example.com/user.php?token=3f0d9sickc0flmg8hnsngk5u07&access_level=application
You can make use of HTTP_ORIGIN as illustrated in the snippet below:
if ( ! array_key_exists( 'HTTP_ORIGIN', $_SERVER ) ) {
$this->referer = $_SERVER['SERVER_NAME'];
} else {
$this->referer = $_SERVER['HTTP_ORIGIN'];
}
I think this method is good..try it
if($_SERVER['HTTP_HOST'] == "localhost"){
define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('SITEPATH', $_SERVER['DOCUMENT_ROOT']);
define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/');
define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/');
}
else{
define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('SITEPATH', $_SERVER['DOCUMENT_ROOT']);
define('TEMPLATE', $_SERVER['DOCUMENT_ROOT'] . '/incs/template/');
define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/');
define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/');
}
$page_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
For more: How to get the full URL of a page using PHP

How can I get the current page's full URL on a Windows/IIS server?

I moved a WordPress installation to a new folder on a Windows/IIS server. I'm setting up 301 redirects in PHP, but it doesn't seem to be working. My post URLs have the following format:
http:://www.example.com/OLD_FOLDER/index.php/post-title/
I can't figure out how to grab the /post-title/ part of the URL.
$_SERVER["REQUEST_URI"] - which everyone seems to recommend - is returning an empty string. $_SERVER["PHP_SELF"] is just returning index.php. Why is this, and how can I fix it?
Maybe, because you are under IIS,
$_SERVER['PATH_INFO']
is what you want, based on the URLs you used to explain.
For Apache, you'd use $_SERVER['REQUEST_URI'].
$pageURL = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
For Apache:
'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
You can also use HTTP_HOST instead of SERVER_NAME as Herman commented. See this related question for a full discussion. In short, you are probably OK with using either. Here is the 'host' version:
'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
For the Paranoid / Why it Matters
Typically, I set ServerName in the VirtualHost because I want that to be the canonical form of the website. The $_SERVER['HTTP_HOST'] is set based on the request headers. If the server responds to any/all domain names at that IP address, a user could spoof the header, or worse, someone could point a DNS record to your IP address, and then your server / website would be serving out a website with dynamic links built on an incorrect URL. If you use the latter method you should also configure your vhost or set up an .htaccess rule to enforce the domain you want to serve out, something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} !(^stackoverflow.com*)$
RewriteRule (.*) https://stackoverflow.com/$1 [R=301,L]
#sometimes u may need to omit this slash ^ depending on your server
Hope that helps. The real point of this answer was just to provide the first line of code for those people who ended up here when searching for a way to get the complete URL with apache :)
$_SERVER['REQUEST_URI'] doesn't work on IIS, but I did find this: http://neosmart.net/blog/2006/100-apache-compliant-request_uri-for-iis-and-windows/ which sounds promising.
Use this class to get the URL works.
class VirtualDirectory
{
var $protocol;
var $site;
var $thisfile;
var $real_directories;
var $num_of_real_directories;
var $virtual_directories = array();
var $num_of_virtual_directories = array();
var $baseURL;
var $thisURL;
function VirtualDirectory()
{
$this->protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$this->site = $this->protocol . '://' . $_SERVER['HTTP_HOST'];
$this->thisfile = basename($_SERVER['SCRIPT_FILENAME']);
$this->real_directories = $this->cleanUp(explode("/", str_replace($this->thisfile, "", $_SERVER['PHP_SELF'])));
$this->num_of_real_directories = count($this->real_directories);
$this->virtual_directories = array_diff($this->cleanUp(explode("/", str_replace($this->thisfile, "", $_SERVER['REQUEST_URI']))),$this->real_directories);
$this->num_of_virtual_directories = count($this->virtual_directories);
$this->baseURL = $this->site . "/" . implode("/", $this->real_directories) . "/";
$this->thisURL = $this->baseURL . implode("/", $this->virtual_directories) . "/";
}
function cleanUp($array)
{
$cleaned_array = array();
foreach($array as $key => $value)
{
$qpos = strpos($value, "?");
if($qpos !== false)
{
break;
}
if($key != "" && $value != "")
{
$cleaned_array[] = $value;
}
}
return $cleaned_array;
}
}
$virdir = new VirtualDirectory();
echo $virdir->thisURL;
Add:
function my_url(){
$url = (!empty($_SERVER['HTTPS'])) ?
"https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] :
"http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
echo $url;
}
Then just call the my_url function.
I use the following function to get the current, full URL. This should work on IIS and Apache.
function get_current_url() {
$protocol = 'http';
if ($_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) {
$protocol .= 's';
$protocol_port = $_SERVER['SERVER_PORT'];
} else {
$protocol_port = 80;
}
$host = $_SERVER['HTTP_HOST'];
$port = $_SERVER['SERVER_PORT'];
$request = $_SERVER['PHP_SELF'];
$query = isset($_SERVER['argv']) ? substr($_SERVER['argv'][0], strpos($_SERVER['argv'][0], ';') + 1) : '';
$toret = $protocol . '://' . $host . ($port == $protocol_port ? '' : ':' . $port) . $request . (empty($query) ? '' : '?' . $query);
return $toret;
}
REQUEST_URI is set by Apache, so you won't get it with IIS. Try doing a var_dump or print_r on $_SERVER and see what values exist there that you can use.
The posttitle part of the URL is after your index.php file, which is a common way of providing friendly URLs without using mod_rewrite. The posttitle is actually therefore part of the query string, so you should be able to get it using $_SERVER['QUERY_STRING']
Use the following line on the top of the PHP page where you're using $_SERVER['REQUEST_URI']. This will resolve your issue.
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'] . '?' . $_SERVER['argv'][0];
Oh, the fun of a snippet!
if (!function_exists('base_url')) {
function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){
if (isset($_SERVER['HTTP_HOST'])) {
$http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$hostname = $_SERVER['HTTP_HOST'];
$dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$core = preg_split('#/#', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
$core = $core[0];
$tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
$end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
$base_url = sprintf( $tmplt, $http, $hostname, $end );
}
else $base_url = 'http://localhost/';
if ($parse) {
$base_url = parse_url($base_url);
if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = '';
}
return $base_url;
}
}
It has beautiful returns like:
// A URL like http://stackoverflow.com/questions/189113/how-do-i-get-current-page-full-url-in-php-on-a-windows-iis-server:
echo base_url(); // Will produce something like: http://stackoverflow.com/questions/189113/
echo base_url(TRUE); // Will produce something like: http://stackoverflow.com/
echo base_url(TRUE, TRUE); || echo base_url(NULL, TRUE); //Will produce something like: http://stackoverflow.com/questions/
// And finally:
echo base_url(NULL, NULL, TRUE);
// Will produce something like:
// array(3) {
// ["scheme"]=>
// string(4) "http"
// ["host"]=>
// string(12) "stackoverflow.com"
// ["path"]=>
// string(35) "/questions/189113/"
// }
Everyone forgot http_build_url?
http_build_url($_SERVER['REQUEST_URI']);
When no parameters are passed to http_build_url it will automatically assume the current URL. I would expect REQUEST_URI to be included as well, though it seems to be required in order to include the GET parameters.
The above example will return full URL.
I have used the following code, and I am getting the right result...
<?php
function currentPageURL() {
$curpageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {
$curpageURL.= "s";
}
$curpageURL.= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$curpageURL.= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$curpageURL.= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $curpageURL;
}
echo currentPageURL();
?>
In my apache server, this gives me the full URL in the exact format you are looking for:
$_SERVER["SCRIPT_URI"]
Reverse Proxy Support!
Something a little more robust. Note It'll only work on 5.3 or greater.
/*
* Compatibility with multiple host headers.
* Support of "Reverse Proxy" configurations.
*
* Michael Jett <mjett#mitre.org>
*/
function base_url() {
$protocol = #$_SERVER['HTTP_X_FORWARDED_PROTO']
?: #$_SERVER['REQUEST_SCHEME']
?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
$port = #intval($_SERVER['HTTP_X_FORWARDED_PORT'])
?: #intval($_SERVER["SERVER_PORT"])
?: (($protocol === 'https') ? 443 : 80);
$host = #explode(":", $_SERVER['HTTP_HOST'])[0]
?: #$_SERVER['SERVER_NAME']
?: #$_SERVER['SERVER_ADDR'];
// Don't include port if it's 80 or 443 and the protocol matches
$port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
return sprintf('%s://%s%s/%s', $protocol, $host, $port, #trim(reset(explode("?", $_SERVER['REQUEST_URI'])), '/'));
}

Categories