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

发布于 2024-08-20 13:14:49 字数 213 浏览 5 评论 0原文

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

两者 不提供完整位置。我应该使用什么来获取完整的 url 位置?

I'm working on this page: 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你没皮卡萌 2024-08-27 13:14:49
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)); }
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)); }
静赏你的温柔 2024-08-27 13:14:49

据我所知,没有本地方法,但你可以使用这个:

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;
}

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;
}
泛滥成性 2024-08-27 13:14:49

例如,如果您尝试将变量添加回通过链接跟踪脚本传递的 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);

在本例中,“link”、“id”和“type”是变量我需要跟踪,但我想要跟踪的 URL 末尾有一个变量,该变量被我的脚本删除,就好像它是发送给它的查询的一部分一样;我需要先将其添加回链接 URL,然后再将其传递给 header("Location:".$link)。

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

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:

$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).

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

静待花开 2024-08-27 13:14:49

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

 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;
  }

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;
  }
很酷又爱笑 2024-08-27 13:14:49

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

$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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文