Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get entire URL, including query string and anchor

Tags:

php

Is there a way to get the entire URL used to request the current page, including the anchor (the text after the # - I may be using the wrong word), in included pages?

i.e. page foo.php is included in bar.php. If I use your solution in foo.php, I need it to say bar.php?blarg=a#example.

like image 780
Alex S Avatar asked Jun 09 '09 00:06

Alex S


People also ask

Does URL include query string?

A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.

Can I pass URL as query parameter?

Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string. but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.

How can URL have multiple query parameters?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".


2 Answers

No, I am afraid not, since the hash (the string including the #) never gets passed to the server, it is solely a behavioural property of the browser. The $_SERVER['REQUEST_URI'] variable will contain the rest however.

If you really need to know what the hash is, you will have to use the document.location.hash JavaScript property, which contains the contents of the hash (you could then insert it in a form, or send it to the server with an ajax request).

like image 155
Alistair Evans Avatar answered Oct 11 '22 05:10

Alistair Evans


// find out the domain: $domain = $_SERVER['HTTP_HOST']; // find out the path to the current file: $path = $_SERVER['SCRIPT_NAME']; // find out the QueryString: $queryString = $_SERVER['QUERY_STRING']; // put it all together: $url = "http://" . $domain . $path . "?" . $queryString; echo $url;  // An alternative way is to use REQUEST_URI instead of both // SCRIPT_NAME and QUERY_STRING, if you don't need them seperate: $url = "http://" . $domain . $_SERVER['REQUEST_URI']; echo $url; 
like image 22
Mihir Bhatt Avatar answered Oct 11 '22 06:10

Mihir Bhatt