Reputation: 1178
I need to retrieve the actual URL that the user see's in their browser. I have an Ajax request running at page load. Hence, the regular $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]
expression returns the request URL of the Ajax request instead of the actual URL in the browser.
Any idea how to get this?
Upvotes: 2
Views: 11057
Reputation: 81
Actual Website Link in php
<?php
echo $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
Upvotes: 1
Reputation: 33865
If you do an Ajax-request, you could pass the address available through Javascripts window.location.href
variable as a POST-variable with the request.
With jQuery it would be something like:
$.ajax({
url: 'your-url.php',
type: "POST",
data: { url: window.location.href },
success: function (data) {
// Do something on success
}
});
With such a request you could access the URL on the server-side with a simple:
<?php
$url = $_POST["url"];
?>
Upvotes: 1
Reputation: 4950
You can't do that with server-side code, as there is no server-side variable that refers to what the client sees. The only thing you CAN see (and then again, it depends on the browser the user's using, some don't pass this info) is the HTTP_REFERRER variable. This however, is only set when a page calls another, not when users first access your site.
See this for more details.
A possible solution however, might be to use javascript function to send the browser's top URL to the server using an AJAX query, and to fire it client-side whenever a user loads the pages) you want to get this info for.
Edit: Damn, too slow, already answered!
Upvotes: 2
Reputation: 615
You could also try using $_SERVER['HTTP_REFERER'];
. This might work, not 100% sure though.
Upvotes: 2
Reputation: 1792
Pass a hidden input that has the browser value set with your ajax request. Unless someone is being malicious, it should suffice.
Upvotes: 1
Reputation: 49425
You could pass it up from javascript in your ajax request, using window.location.href
.
Also, it's likely that $_SERVER['HTTP_REFERER']
will contain the browser's current location.
Upvotes: 6
Reputation: 37701
Server-side languages can't see what happens after they've rendered and outputted the page.
Upvotes: 0