PHP 确定当前 url

发布于 2024-08-14 19:03:58 字数 329 浏览 1 评论 0原文

我需要修改我的函数以返回我所在的当前文件夹。这是我当前的函数:

function getLinkFromHost($url){  
    $port = $_SERVER['REMOTE_PORT'];  
    $server = $_SERVER['HTTP_HOST'];  
    if($port == 443){  
        $type = "https";  
    } else {  
        $type = "http";  
    }  
    return $type . "://" . $server . "/" . $url;  
}

I need to modify my function to return also the current folder I am in. Here is my current function:

function getLinkFromHost($url){  
    $port = $_SERVER['REMOTE_PORT'];  
    $server = $_SERVER['HTTP_HOST'];  
    if($port == 443){  
        $type = "https";  
    } else {  
        $type = "http";  
    }  
    return $type . "://" . $server . "/" . $url;  
}

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

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

发布评论

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

评论(5

何以笙箫默 2024-08-21 19:03:58

查看 $_SERVER['REQUEST_URI']$_SERVER['SCRIPT_NAME']

(来自 $_SERVER 手动输入)

Take a look at $_SERVER['REQUEST_URI'] or $_SERVER['SCRIPT_NAME']

(From the $_SERVER manual entry)

乞讨 2024-08-21 19:03:58

这是一个简短而甜蜜的函数,我已经用它来做这件事有一段时间了。

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

我不能相信,它属于:

http://www.webcheatsheet.com/ PHP/get_current_page_url.php

Here a short sweet function I've been using to do this for awhile now.

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

I can't take the credit, it belongs to:

http://www.webcheatsheet.com/PHP/get_current_page_url.php

相思故 2024-08-21 19:03:58

也许您还想将 get vars 包含到您的网址中,因此您应该向 highphilosopher 函数添加一些内容...

$current_url = rtrim(curPageURL(), "/").(!empty ($_GET)) ? "?".http_build_query($_GET) : "";

Probably you also want to include get vars into your url, so you should add something to highphilosopher function...

$current_url = rtrim(curPageURL(), "/").(!empty($_GET)) ? "?".http_build_query($_GET) : "";

终陌 2024-08-21 19:03:58

…………

echo $_SERVER['PHP_SELF']; // return current file

echo __FILE__; // return current file

echo $_SERVER['SCRIPT_NAME']; // return current file

echo dirname(__FILE__); // return current script's folder

// etc

..........

echo $_SERVER['PHP_SELF']; // return current file

echo __FILE__; // return current file

echo $_SERVER['SCRIPT_NAME']; // return current file

echo dirname(__FILE__); // return current script's folder

// etc
魂牵梦绕锁你心扉 2024-08-21 19:03:58

这是一个可能有帮助的方法:

function current_url()
{
    $result = "http";

    if($_SERVER["HTTPS"] == "on") $result .= "s";

    $result .= "://".$_SERVER["SERVER_NAME"];

    if($_SERVER["SERVER_PORT"] != "80") $result .= ":".$_SERVER["SERVER_PORT"];

    $result .= $_SERVER["REQUEST_URI"];

    return $result;
}

Here's a method that might help:

function current_url()
{
    $result = "http";

    if($_SERVER["HTTPS"] == "on") $result .= "s";

    $result .= "://".$_SERVER["SERVER_NAME"];

    if($_SERVER["SERVER_PORT"] != "80") $result .= ":".$_SERVER["SERVER_PORT"];

    $result .= $_SERVER["REQUEST_URI"];

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