duanqi5114 2013-12-25 21:48
浏览 67
已采纳

从查询字符串和锚片段中拆分基本URL

Let's say I have this URL:

http://test.com/directory-test/?id=4746&other=454#comments

How can I retrieve the URL of that specific page with PHP, however, just without all of the variables and #s. So for example:

http://test.com/directory-test/

I found this function, however, it includes EVERYTHING in the URL, so it returns a URL that looks like the first example above, but I want a URL that looks like the second example.

function curPageURLALL() {
 $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;
}
  • 写回答

1条回答 默认 最新

  • dpzff20644 2013-12-25 21:52
    关注

    Use PHP_SELF instead of REQUEST_URI :)

    function curPageURL() {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["PHP_SELF"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"];
        }
        return $pageURL;
    }
    

    Hymm, PHP_SELF might return also the file example index.php so maybe this

    function curPageURL() {
        $pageURL = 'http';
        if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        $pageURL .= $_SERVER["SERVER_NAME"];
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= ":".$_SERVER["SERVER_PORT"];
        }
        $pos = strpos($_SERVER["REQUEST_URI"],"?");
        $pageURL .= ($pos===false)?$_SERVER["REQUEST_URI"]:substr($_SERVER["REQUEST_URI"],0,$pos);
        return $pageURL;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度