获取当前页面的完整 URL (PHP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2236873/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:41:25  来源:igfitidea点击:

Getting the full URL of the current page (PHP)

phpscriptingpathlocation

提问by eozzy

I'm working on this page: http://localhost/projectname/custom.php

我正在这个页面上工作: http://localhost/projectname/custom.php

Both <?php echo $_SERVER['REQUEST_URI']; ?>and <?php echo $PHP_SELF; ?>don't give full location. What should I use to grab the full url location?

双方<?php echo $_SERVER['REQUEST_URI']; ?><?php echo $PHP_SELF; ?>没有充分的位置。我应该用什么来获取完整的 url 位置?

回答by Sorantis

function selfURL() 
{ 
    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; 
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; 
} 

function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }

回答by harpax

There isn't a native method as far as I know, but you could use this:

据我所知,没有本地方法,但您可以使用它:

function curPageURL() {
 $pageURL = 'http';
 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;
}

回答by Nancy Hastings-Trew

If you are trying to add variables back onto the end of an URL that you are passing through a link tracking script, for example, you could try this:

例如,如果您尝试将变量添加回通过链接跟踪脚本传递的 URL 的末尾,您可以尝试以下操作:

$URI = array();
foreach($_GET as $key=>$val)
{
   if ($key!="link"&&$key!="id"&&$key!="type") $URI[] = "$key=".urlencode($val);
}
if (sizeof($URI)>0) $link.="&".join("&",$URI);

In this case, "link", "id" and "type" were the variables I needed for the tracking, but the URL I wanted to track had a variable on the end of it that got stripped off by my script as if it was part of the query being sent to it; I needed the add it back to the link URL before passing it to header("Location:".$link).

在这种情况下,“link”、“id”和“type”是我进行跟踪所需的变量,但我想跟踪的 URL 末尾有一个变量,该变量被我的脚本剥离,好像它是发送给它的查询的一部分;在将它传递给 header("Location:".$link) 之前,我需要将它添加回链接 URL。

If this is what you are trying to achieve this works great and is shorter than above example.

如果这是您要实现的目标,那么效果很好,并且比上面的示例更短。

回答by Muhammad Ashikuzzaman

I found this code very helpful

我发现这段代码非常有帮助

$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === 
FALSE ? 'http' : 'https';            // Get protocol HTTP/HTTPS
$host     = $_SERVER['HTTP_HOST'];   // Get  www.domain.com
$script   = $_SERVER['SCRIPT_NAME']; // Get folder/file.php
$params   = $_SERVER['QUERY_STRING'];// Get Parameters occupation=odesk&name=ashik

$currentUrl = $protocol . '://' . $host . $script . '?' . $params; // Adding all

echo $currentUrl;

回答by Teja Kantamneni

check this one... a bit long and dirty but works good...

检查这个......有点长而且脏,但效果很好......

 function absolutizeUrl ( $u, $p )
 {
    $url = parse_url( $u );
    $page = parse_url( $p );

    if ( strpos( $u , '/' ) === 0 )
    {
            //already absolute              
    } else {
            $basePath = '';
            if (
                    isset( $page[ 'path' ] )
                    && strpos( ltrim( $page[ 'path' ], '/' ), '/' )
            )
            {
                    $baseTokens = explode( '/', $page[ 'path' ] );
                    array_pop( $baseTokens ); // strip basename                     
                    $baseTokens[] = $u;
                    $u = join( '/', $baseTokens );
            }
    }
    if ( ! isset( $url[ 'host' ]))
    {
            $u = 'http://'.$page[ 'host' ].'/'.ltrim( $u, '/' );
    }
    return $u;
  }