如何使用PHP从地址栏中检索完整的URL


How to retrieve complete URL from address bar using PHP?

可能的重复项:
如何使用PHP获取地址栏上的完整URL

我使用此功能,但它并非一直有效。谁能给出提示?

function sofa_get_uri() {
    $host = $_SERVER['SERVER_NAME'];
    $self = $_SERVER["REQUEST_URI"];
    $query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
    $ref = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
    return $ref;
}

我想检索地址栏中的链接(确切地说),以便在用户注销时使用它来引用用户。网址不同:

http://domain.com/sample/address/?arg=bla
http://domain.com/?show=bla&act=bla&view=bla
http://domain.com/nice/permalinks/setup

但是我无法获得适用于所有情况的功能并给我真正的推荐人。

请提示。

这个怎么样?

function getAddress() {
    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
echo getAddress();

您可以使用上面的函数来检索 URL 直到 GET 参数。所以你有像 = 'localhost/site/tmp' 这样的字符串(示例)。之后,如果无法使其他任何内容正常工作,您可以遍历 GET 参数。手动在字符串末尾添加"?"。

$str = 'localhost/site/tmp/?'
foreach ($_GET as $key => $value) {
    $str .= $key.'='.$value.'&';
}
substr_replace($str, "", -1);

回声$str;最后,您将删除最后一个符号,该符号为"&"且不需要。