How to send current url to server?
I would like to have a submit button on 开发者_运维知识库other people's HTML pages (with permission). When it is clicked I would like the code on that page to grab the url of that page, and send that URL to my server (the user will not be sent/linked to another page). Is this possible using only HTML and PHP?
-- Edit --
I COULD link to another page as long as my server receives the url first, if that helps.
Is there any way to do this with just the html on the client page and php on my server?
Using PHP, you could read the $_SERVER['HTTP_REFERER']
variable, but that's less reliable than using Javascript to fetch the current window.location
and post that for instance. By the way, you need JS anyway to send the post request by Ajax if you don't want to 'redirect' the user.
assuming that you have jQuery there:
<div id="track_button">Send URL</div>
<script>
function send_url(){
$("body").append("<iframe src=\"http://yourdomain/script.php?url=" + encodeURIComponent(document.URL) + "\" style=\"width: 0px; height: 0px; border: none;\"></iframe>");
}
$(document).ready(function(){
$("#track_button").button().click(function(){
send_url();
});
});
</script>
"When it is clicked I would like the code on that page to grab the url of that page" this sentence implies following:
1) there is element, published on the person's X site
2) "click" is usually made by human, most of the browsers support javascript - cannot you really use it?
3) "grab url" -- there are two methods: 1) use javascript; 2) relay on http_referrer as others suggested
So, I have provided solution, where javascript (+jQuery) is used. jQuery can be dropped, and same effect can be achieved with plain JavaScript. If you are not permitted to put JavaScript, http_referrer is only thing that you can relay on. But it's quite unreliable, as quite a few people have that feature turned off.
Investigate using a bookmarklet to enable the user to send data about their web page to your server.
As an example of this sort of thing, look into Amazon's bookmarklet that enables you to add stuff from any site to your Amazon wishlist--same sort of idea.
精彩评论