How to get full URL on the address bar using PHP [duplicate]
Possible Duplicate:
Get entire URL, including query string and anchor
I have my url like www.domain.com/#!/username (just like in twitter).
How can I retrieve the full URL (the whole string above) including the substring after '#!' in the address using PHP or Javascript??
In JavaScript, unless you've got something else going on, you should be able to get the full URL from document.location
, (so var URLstring = document.location;
for example).
In PHP, as others have noted, this is impossible due to the way the hash works (it navigates within the page, and does not trigger a page-reload so the server never knows about it, unless the JS calls an Ajax function that triggers a script on the server).
The hash part of the URL (the # and everything after) isn't sent by the browser to the server, so it's not available to PHP.
If you absolutely need your server side to be aware of it, the only way is to load an intermediate page containing Javascript that reads the hash value, and makes another request where the value is contained in a regular HTTP parameter. E.g. the intermediate page could be something like:
<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>
The part after the hash (#) is never sent to the server so you will never receive the "full" url back to PHP. The part after the # is only used on the client side and can only be sent from a server to the client.
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('"', '"',$theURI);
$theURI = str_replace('<', '<',$theURI);
$theURI = str_replace('>', '>',$theURI);
$theURI = preg_replace('/eval\((.*)\)/', '', $theURI);
$theURI = preg_replace('/[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']/', '""', $theURI);
}
echo (string)$theURI;
}
Sled's answer is the best you can do, however, there is no way to get text after a pound sign "#". Anything after a "#" is considered an anchor and is not sent to the server. The "#" is only interpreted by the browser. You might try some Javascript trickery here, but I recommend avoiding passing data after a "#".
精彩评论