Equivalent to escape (window.location.href) in PHP

I tried so many combinations to get the correct path to my page, for example javascript code

escape(window.location.href) 

I tried this:

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

but it returns something like:

 http://www.mydomain.com/node/4158?asdf=1asdf=1 

when the actual page:

 http://www.mydomain.com/node/4158?asdf=1 

and if I have this URL: http://www.mydomain.com/node/4158#comments for example, the result:

 http://www.mydomain.com/node/4158 

i.e. without "#comments"

+4
source share
5 answers

Fragment identifier # processed entirely on the client side. The browser will not send it to the server, so PHP will not be able to access it.

The only way to find out what it is is to include JavaScript in the page that makes the HTTP request, including the full URI.

+3
source

try

 $url = $_SERVER['REQUEST_URI']; 
0
source

$_SERVER['QUERY_STRING'] - a substring $_SERVER['REQUEST_URI'] - you just need the last one. But instead of creating the URL yourself, just use $_SERVER['SCRIPT_URI'] .

In addition, since your application is a server application, and the fragment identifier (everything from # ) is not included in HTTP requests, there is no way to access it in php code.

0
source
 $location = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://"; if ($_SERVER["SERVER_PORT"] != "80") { $location .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; } else { $location .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; } echo $location; 
0
source

Many people say that the fragment here is client-side, but according to the docs parse_url will return the fragment if it is present (see the first example in the link).

0
source

Source: https://habr.com/ru/post/1398571/


All Articles