PHP - How to get Current URL  screenshot

PHP - How to get Current URL

About PHP:

PHP is an acronym for Hyper Text Preprocessor and it is an open source general purpose server side scripting language which is most widely used in web development. In this article we will explain how to get a current server URL.

Before starting, we need to understand about variables in PHP as variables are one of the fundamental building blocks of any programming language including PHP. A variable is simply container which can hold the information or data. Variables are created by the programmer and depend upon the scope of the information or data in php script, while php provides a large number of predefined variables.

As you know, this article is about “ how to get a current server URL”, so we need to understand about php predefined variables, and especially $_SERVER predefined variables.

The following are predefined variables:

$GLOBALS, $_SERVER, $_GET, $_POST, $_FILE, $_COOKIES,

$_SESSION, $_REQUEST, $_ENV

Note: Predefined variables are also known as “superglobals

The $_SERVER variables ( array) contains information about a server, such as the server name, paths and script locations.

Using the $_SERVER predefined variables, we can get the current server URL by passing parameter REQUEST_URI.

Example: 1 How to get current server URL.

<?php
$Get_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URl'];
echo $Get_url;
?>