简体   繁体   English

如何在变量中存储包含哈希标记的完整URL?

[英]How can I store a full url that contains a hashmark within a variable?

I'm trying to store a url such as: 我正在尝试存储如下网址:

http://localhost/pro_print/index.php#page=home 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. 我发现了很多解决方案来存储index.php,但其中不包括井号及其后面的内容。 Is there a PHP solution for this? 是否有针对此的PHP解决方案?

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? 我确实看到我可以使用document.write(document.url)document.write(location.href)使用javaScript获得完整的URL,包括哈希标记,但是如何将其存储到变量中? Is there any way I can combine PHP with javaScript in some sort of solution like this? 在这种解决方案中,有什么方法可以将PHP与javaScript结合使用?

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

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. 片段标识符(#及其后面出现的所有内容)完全由客户端处理,并且在请求URI时不发送给服务器。

To make it available to PHP, you would have to: 要使其可用于PHP,您必须:

  1. Allow the page to load 允许页面加载
  2. Read the location with JavaScript 使用JavaScript读取位置
  3. Send it to the server using an Ajax technique (eg XMLHttpRequest) or in a subsequent request 使用Ajax技术(例如XMLHttpRequest)或在后续请求中将其发送到服务器

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 (eg the query string). 一种替代方法是在URI中其他位置(例如查询字符串)中复制片段标识符中的信息。 This is used by this site when submitting an answer. 提交答案时,此站点将使用此地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM