PHP scripts are used to retrieve the URL of a web page on the web server. There are multiple approaches to achieve this task. In this article, you will explore three approaches to get full URL in PHP. You will use the $_SERVER superglobal array variable and its various elements to retrieve different parts of the URL. After that, those parts will be appended together to get full URL in PHP. In this way, you will get the full path of the current web page.Bạn đang xem: Php get full url code example
The necessary superglobal variables such as $_SERVER, $_SERVER, $_SERVER are used to get full URL in PHP. The variable HTTPS can easily retrieve the protocol in the URL of a webpage. If it returns a value “on”, then the protocol is HTTPS. And if the value returned is not equal to “on” then the protocol in the URL of the webpage is HTTP.
Đang xem: Php: get keywords from search engine referer url
What Are Superglobals?
PHP provides you with certain specific pre-defined variables. These variables help you retrieve the data and information about a request or its context. As their name suggests, superglobal variables have a superglobal scope. They are accessible throughout your PHP script. You can access these variables by using any function, file, or class without doing much work to create or declare a new variable inside your function or class. The primary purpose that these variables serve is to hold as well as retrieve the data from a page of an application.
Here is a list of superglobals that are provided by PHP:
It is a type of superglobal variable that helps to access all the global variables throughout the PHP script and comes in useful when trying to get full URL in PHP. PHP provides an array $GLOBALS to hold the references of all the global variables as array elements. The index of the array represents the names of the global variables.
In the program stated above, two global variables, var1 and var2 are initialized with 2 integer values. The function getSum() stores the result of the addition of these two global variables in a GLOBAL array.
Server is a PHP superglobal that is defined as an array that contains the data about paths, headers, script location, etc. The web server itself makes these entries within the array. Using this to get full URL in PHP however depends on your server, as not every server provides that information. Some may skip them, whereas others may provide some extra data. The data provided by your web server is sometimes used to retrieve the data from a $_SERVER superglobal variable
The following program illustrates the $_SERVER variable in PHP.
// print the name of the server
// print the name of the host
// print the complete URL of the current page
// print the user agent string
// print the path of the script
In the program depicted above, various $_SERVER elements are used to retrieve information about the web page on the server. For example, $_SERVER is used to get the name of the current file, $_SERVER is used to get the name of the server, $_SERVER returns the name of the host, and so on.
Request is another superglobal whose function is to collect and store the data whenever you submit an HTML form. This is one of the more common methods to get full URL in PHP. The $_REQUEST array stores the information about $_GET, $_POST and $_COOKIE by default. However, $_REQUEST is not in much use as the same purpose can be fulfilled by $_GET and $_POST.