Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the full URL in PHP

Tags:

url

php

I use this code to get the full URL:

$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; 

The problem is that I use some masks in my .htaccess, so what we see in the URL is not always the real path of the file.

What I need is to get the URL, what is written in the URL, nothing more and nothing less—the full URL.

I need to get how it appears in the Navigation Bar in the web browser, and not the real path of the file on the server.

like image 734
DiegoP. Avatar asked Jul 20 '11 21:07

DiegoP.


People also ask

How we can get URL in PHP?

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

How do I get a full URL?

Just right-click in Chrome's address bar select “Always show full URLs” to make Chrome show full URLs. Chrome will now always show the full URL of every web address you open. To disable this feature, right-click in the address bar again and uncheck it.

How get slug URL in PHP?

$slugs = explode("/", $_GET['params']); This will give you an array filled with every element in your URL. This allows you to use each element as you require.

How can I get the last part of a URL in PHP?

you can use basename($url) function as suggested above. This returns the file name from the url. You can also provide the file extension as second argument to this function like basename($url, '.


1 Answers

Have a look at $_SERVER['REQUEST_URI'], i.e.

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 

Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

like image 177
ax. Avatar answered Sep 21 '22 12:09

ax.