php 获取整个 URL,包括查询字符串和锚点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/967649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get entire URL, including query string and anchor
提问by Alex S
Is there a way to get the entire URL used to request the current page, including the anchor (the text after the # - I may be using the wrong word), in included pages?
有没有办法在包含的页面中获取用于请求当前页面的整个 URL,包括锚点(# 之后的文本 - 我可能使用了错误的词)?
i.e. page foo.php is included in bar.php. If I use your solution in foo.php, I need it to say bar.php?blarg=a#blahblahblah
即页面 foo.php 包含在 bar.php 中。如果我在 foo.php 中使用你的解决方案,我需要它说 bar.php?blarg=a#blahblahblah
回答by Alistair Evans
No, I am afraid not, since the hash (the string includingthe #) never gets passed to the server, it is solely a behavioural property of the browser. The $_SERVER['REQUEST_URI']variable will contain the rest however.
不,恐怕不会,因为哈希(包括#的字符串)永远不会传递到服务器,它只是浏览器的行为属性。然而,$_SERVER['REQUEST_URI']变量将包含其余部分。
If you really need to know what the hash is, you will have to use the document.location.hashJavaScript property, which contains the contents of the hash (you could then insert it in a form, or send it to the server with an ajax request).
如果你真的需要知道哈希是什么,你将不得不使用document.location.hashJavaScript 属性,它包含哈希的内容(然后你可以将它插入到表单中,或者通过 ajax 请求将它发送到服务器)。
回答by Mihir Bhatt
// find out the domain:
$domain = $_SERVER['HTTP_HOST'];
// find out the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
// find out the QueryString:
$queryString = $_SERVER['QUERY_STRING'];
// put it all together:
$url = "http://" . $domain . $path . "?" . $queryString;
echo $url;
// An alternative way is to use REQUEST_URI instead of both
// SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
$url = "http://" . $domain . $_SERVER['REQUEST_URI'];
echo $url;
回答by Alex Weinstein
You can pass up the full URL, including the anchor (the part after the #), using a Javascript onload function that sends that URL to an Ajax endpoint.
您可以使用将 URL 发送到 Ajax 端点的 Javascript onload 函数传递完整 URL,包括锚点(# 之后的部分)。
回答by karim79
You can't - you'll have to write the value of the hash to a cookie via Javascript to send it to the server on the subsequent request.
您不能 - 您必须通过 Javascript 将哈希值写入 cookie,以便在后续请求中将其发送到服务器。
回答by Elshan
This example show's full URL request with portor https
此示例显示带有port或的完整 URL 请求https
function curPageURL() {
$pageURL = 'http';
if(isset($_SERVER["HTTPS"]))
if ($_SERVER["HTTPS"] == "on") {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
}
return $pageURL;
}
usage : echo curPageURL();
用法 : echo curPageURL();
回答by gy134340
It is true that you can't pass hash data to server. but sometimes what you want is get some url with '#blablabla' from the server, like when you share link to someone with '#', but the site need to login. below is another way to think, maybe not vary detailed.
确实,您无法将哈希数据传递给服务器。但有时您想要的是从服务器获取一些带有“#blablabla”的网址,例如当您与带有“#”的人共享链接时,但该站点需要登录。下面是另一种思考方式,可能不详。
First, sometimes you want to share your link with '#',like:
首先,有时您想用“#”分享您的链接,例如:
www.so.com#lala
First, you can change your url use javascript and pass some data with '?' and '#' at the same time, like:
首先,您可以使用 javascript 更改您的 url 并使用 '?' 传递一些数据。和 '#' 同时,例如:
www.so.com?foo=lala&&flag=hash#lala
then, as the '#' nerver pass to the server, but you can get the data from $_GET[''],like:
然后,作为 '#' 神经传递到服务器,但您可以从 $_GET[''] 获取数据,例如:
if($_GET['flag'] === 'hash'){
// base url
$_SESSION['url'] = strtok('http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"],"?");
// hash data
$_SESSION['hash'] = $_GET['foo'];
}
then, you can do everything with the '#' data, if you want to pass it to client, you can:
然后,您可以使用 '#' 数据做任何事情,如果您想将其传递给客户端,您可以:
$url = $_SESSION['url']."#".$_SESSION['hash'];
header("Location:".$url);
At last, the url is back
终于,网址回来了
回答by user1175283
You can pass the anchor using rawurlencode funcion.
您可以使用 rawurlencode 函数传递锚点。
Basically you just need to do something like this:
基本上你只需要做这样的事情:
bar.php?blarg=a<?php echo rawurlencode('#blahblahblah')?>

