How to get full url in address bar using PHP

Possible duplicate:
Get the entire URL, including the query string and binding

I have my own url like www.domain.com/#!/username (like on twitter).

How to get the full URL (the whole line above), including the substring after '#!' in address using PHP or Javascript ??

+4
source share
5 answers

In JavaScript, if you don't have something else, you can get the full URL from document.location , (e.g. var URLstring = document.location; ).

In PHP, as others have noted, this is not possible due to how the hash works (it moves inside the page and does not cause the page to reload, so the server never knows about it unless JS calls Ajax the function that runs the script on the server).

+4
source

The part after the hash (#) is never sent to the server, so you will never get the "full" URL back to PHP. The part after # is used only on the client side and can only be sent from the server to the client.

+1
source

The hash part of the URL (# and all after) is not sent by the browser to the server, so it is not available for PHP.

If you absolutely need your server side to know about this, the only way is to load an intermediate page containing Javascript that reads the hash value and makes another request where that value is contained in a regular HTTP parameter. For instance. an intermediate page might look something like this:

 <html> <!-- snip.. --> <body> <script type="text/javascript"> $(document).ready(function() { $.get('realContent.php?hash=' + location.hash, function(data) { $('#content').html(data); } ); } </script> <div id="content"></div> </body> </html> 
+1
source

Sled's answer is the best you can do, however there is no way to get the text after the pound sign "#". Everything that is indicated after the "#" is considered an anchor and is not sent to the server. "#" Only interpreted by the browser. You can try some Javascript tricks here, but I recommend avoiding passing data after "#".

0
source

I used this function for this (PHP 5.2):

 function getInstance($uri = 'SERVER') { if ($uri == 'SERVER') { if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) { $https = 's://'; } else { $https = '://'; } if (!empty ($_SERVER['PHP_SELF']) && !empty ($_SERVER['REQUEST_URI'])) { $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if (strlen($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING']) === false) { $theURI .= '?'.$_SERVER['QUERY_STRING']; } } else { $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']; if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { $theURI .= '?' . $_SERVER['QUERY_STRING']; } } $theURI = urldecode($theURI); $theURI = str_replace('"', '&quot;',$theURI); $theURI = str_replace('<', '&lt;',$theURI); $theURI = str_replace('>', '&gt;',$theURI); $theURI = preg_replace('/eval\((.*)\)/', '', $theURI); $theURI = preg_replace('/[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']/', '""', $theURI); } echo (string)$theURI; } 
0
source

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


All Articles