Last Updated on April 16, 2021 by admin
Do you really want to get your current page URL? you can use the built-in variable of PHP. This is a superglobal variable. On this page, glad to share how to get the full url of my current page in PHP?
Which is called $_SERVER[‘HTTPS’]; You can easily get the url using this super global variable $_SERVER[‘HTTPS’];
First of all, I want to tell you how to write PHP code in PHP.
Here are few steps to get the full URL of the current page which are given below:
The PHP programming very helps to get the complete URL of currently running pages.
- Create a PHP variable that will store the URL in string formation.
- look over there whether the HTTPS is enabled by the server or not. If it is enabled, append “HTTPS” to the URL string. If HTTPS is not enabled, append “HTTP” to the URL string.
- include the regular symbol, i.e. “://” to the URL.
- Add the HTTP_HOST(which we have requested. for the host for example e.g. www.google.com, www.yourdomain.com, etc…)alike name of the server.
- include the REQUEST_URI(The resource which we have requested, e.g. /index.php, etc…) to the URL string.
Note: The isset() function is used here to check whether HTTPS is enabled or not. It checks whether a variable exists or not.
The situation of HTTPS is saved in the Global variable $_SERVER[‘HTTPS’]. So, use the $_SERVER[‘HTTPS’] into isset() function this function is used to check whether it exists or not. This will be telling us whether HTTPS is enabled or not. please check the value of $_SERVER[‘HTTPS’]. If it is “no”, then the HTTPS is enabled and we have to include “HTTPS” to the URL. This is method number one see the example below:
METHOD NO 1: get the full url of my current page in PHP
<?php // Program to display URL of current page. if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') $link = "https"; else $link = "http"; // Here append the common URL characters. $link .= "://"; // Append the host(domain name, ip) to the URL. $link .= $_SERVER['HTTP_HOST']; // Append the requested resource location to the URL $link .= $_SERVER['REQUEST_URI']; // Print the link echo $link; ?>
You have seen the first method now we are going to start the next method so, we can also get the complete URL of the current page using another way given in the next example.
<?php // Program to display current page URL. $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; echo $link; ?>
If you want to get the only name which the current page opened at browser. Here you must visit out: Java also has a good guide to get the full url of my current page in PHP.
<?php $curPageName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); echo "This is a current page URL: ".$curPageName; echo "</br>"; ?>
Read more: Include VS require in PHP