In this tutorial we will show you the solution of PHP get current URL path, the tutorial teaches how to use the PHP programming language to acquire the current full URL.
The complete URL of PHP can be useful in a variety of situations. The contents of the webpage must be rendered based on query parameters.
It's possible that the entire page will need to be redirected or refreshed.
Step By Step Guide On PHP Get Current URL Path :-
Method 1 - Using a Conditional Statement, display the new page's URL.
To obtain a full URL in PHP, divide every web page's URL into three parts: the protocol (HTTP or HTTPS), the domain name, and the URL of the resource location.
In the programme shown above, you will get the current page's URL in three parts and then combine them to get the complete URL.
The following programme shows how to use if-else statements to display the current page's URL
<?php $domName = $_SERVER['HTTP_HOST']; $sourceURI = $_SERVER['REQUEST_URI']; if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') $urlProtocol = "https"; else $urlProtocol = "http"; $urlAddress = $urlProtocol."://".$domName.$sourceURI; echo "<h3 style='color:Red'>This is the URL address of the current page : </h3><br>"; echo $urlAddress; ?>
- This software makes use of superglobal variables. The $_SERVER['HTTP HOST'] variable is used to read the domain name of the current web page's URL.
- Save the domain name that was returned in a variable. The $_SERVER['REQUEST URI'] variable returns the URI of the resource's requested location. In a variable, save the returned value.
- $_SERVER['HTTPS'] is a superglobal variable that returns the protocol in a web page's URL.
- The conditional operator is used to determine whether the current web page's protocol is HTTP or HTTPS.
- The isset() method first determines whether or not the superglobal variable $_SERVER['HTTPS'] is set.
- The protocol of the web page is HTTPS if the value returned by the superglobal variable is equal to "on," otherwise it is HTTP.
Method 2 - Using the Ternary Operator, display the new page's URL.
This example of getting the entire URL in PHP uses the same strategy as the previous example.
The only difference is that instead of utilising if-else expressions, you utilise the ternary operator this time.
<?php $urlProtocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http"); $domName = $_SERVER['HTTP_HOST']; $sourceURI = $_SERVER['REQUEST_URI']; $urlAddress = $urlProtocol."://".$domName.$sourceURI; echo "<h3 style='color:Red'>This is the URL address of the current page : </h3><br>"; echo $urlAddress; ?>
- The isset() method checks the requested page's protocol, and if the value is set to "on," the ternary operator returns HTTPS; otherwise, it returns HTTP.
- The returned output is saved in a variable, which is then concatenated with the rest of the URL that the application has retrieved.
- The ternary operator used in the above programme to display the current page's URL.
In PHP programming, this tutorial highlighted some of the most effective techniques to acquire the current URL.
The examples will undoubtedly assist you in comprehending the format, process, and syntaxes employed.
The super global variable $_SERVER is the most common method for obtaining the current URL. You have complete control over the syntax format while using this method.
However, as comparison to the others, this procedure is a little more complicated.
Now it's up to you to select which technique of obtaining the current URL best meets your PHP programming needs. I hope this tutorial on PHP get current URL path helps you.