PHP scripts are used to retrieve the URL of a website page on the web server. There are multiple approaches khổng lồ achieve this task. In this article, you will explore three approaches khổng lồ 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 lớn 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<‘HTTPS’>, $_SERVER<‘REQUEST_URI’>, $_SERVER<‘SERVER_PORT’> are used lớn 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. Và if the value returned is not equal to lớn “on” then the protocol in the URL of the webpage is HTTP.
What Are Superglobals?
PHP provides you with certain specific pre-defined variables. These variables help you retrieve the data và 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 các mục of superglobals that are provided by PHP:
It is a type of superglobal variable that helps to lớn access all the global variables throughout the PHP script và comes in useful when trying to get full URL in PHP. PHP provides an array $GLOBALS< > khổng lồ hold the references of all the global variables as array elements. The index of the array represents the names of the global variables.
The following program illustrates the $GLOBALS variable in PHP.
In the program stated above, two global variables, var1 và 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 hệ thống itself makes these entries within the array. Using this to lớn get full URL in PHP however depends on your server, as not every hệ thống provides that information. Some may skip them, whereas others may provide some extra data. The data provided by your web vps is sometimes used lớn 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 khổng lồ retrieve information about the website page on the server. For example, $_SERVER<'PHP_SELF'> is used to lớn get the name of the current file, $_SERVER<'SERVER_NAME'> is used khổng lồ get the name of the server, $_SERVER<'HTTP_HOST'> returns the name of the host, và so on.
Request is another superglobal whose function is to lớn collect and store the data whenever you submit an HTML form. This is one of the more common methods lớn get full URL in PHP. The $_REQUEST< > array stores the information about $_GET, $_POST và $_COOKIE by default. However, $_REQUEST is not in much use as the same purpose can be fulfilled by $_GET and $_POST.