How can I store a full url that contains a hashmar

2020-05-03 20:35发布

I'm trying to store a url such as:

http://localhost/pro_print/index.php#page=home

in a variable, but I can't find a function that does that. I found plenty of solutions to store the index.php, but it doesn't include the hashmark and what follows it. Is there a PHP solution for this?

I did see that I can get the full url including hashmark with javaScript using document.write(document.url) or document.write(location.href) but how do I store that into my variable? Is there any way I can combine PHP with javaScript in some sort of solution like this?

<?php $url ="?><script>document.write(document.url)</script><?php "?>

1条回答
Melony?
2楼-- · 2020-05-03 21:15

The fragment identifier (the # and everything that appears after it) is handled entirely client side, and is not sent to the server when the URI is requested.

To make it available to PHP, you would have to:

  1. Allow the page to load
  2. Read the location with JavaScript
  3. Send it to the server using an Ajax technique (e.g. XMLHttpRequest) or in a subsequent request

This won't make it available to the server at the time the original script runs, but nothing else can.

An alternative approach would be to duplicate the information in the fragment identifier somewhere else in the URI (e.g. the query string). This is used by this site when submitting an answer.

查看更多
登录 后发表回答