【问题标题】:Get url information without GET or POST无需 GET 或 POST 即可获取 url 信息
【发布时间】:2016-07-20 13:32:40
【问题描述】:

我有这个网址

http://www.mywebsite.com/person?id=10

但我不想要 $_GET 变量。 我想要这样:

http://www.mywebsite.com/person/10

【问题讨论】:

  • 你应该寻找关于 URL 重写的教程
  • url 重写是正确的答案,但是请注意,它只在一个方向上起作用:用户输入“example.com/person/10”并且重写会将其更改为“example.com /person?id=10"
  • URL rewriting with PHP的可能重复

标签: php url post get url-redirection


【解决方案1】:

您可以使用 $_SERVER['REQUEST_URI'] 并且您可以从“人”中爆炸(如果可以解决)

$uri_parts = explode('person', $_SERVER['REQUEST_URI'], 2);
echo $uri_parts[1]; // will return /10

【讨论】:

  • 将返回 10...不,它会返回 /10
  • 是的,这确实会返回,但@Allamanda 想从 URL 中减去值,而不是在获取值后使用 GET 函数,可以从斜杠和所有内容中过滤。
【解决方案2】:

下面是一个方便的小功能,您可以在这种情况下使用。您可能想测试代码here

<?php

    $currentURL = getCurrentPageURL();   //<= GET THE ACTIVE PAGE URL
    $cleanURL   = getPreFormattedURI($currentURL);

    var_dump($cleanURL);

    // FUNCTION TO AUTOMATICALLY GET THE ACTIVE PAGE URL
    function getCurrentPageURL() {
        $pageURL = 'http';

        if ((isset($_SERVER["HTTPS"])) && ($_SERVER["HTTPS"] == "on")) {
            $pageURL .= "s";
        }
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
        }else {
            $pageURL .= $_SERVER["SERVER_NAME"];
        }
        $pageURL .= $_SERVER["REQUEST_URI"];
        return $pageURL;
    }

    // FUNCTION THAT FORMATS THE URL THE WAY YOU SPECIFIED
    function getPreFormattedURI($uri, $key="id"){
        $objStripped            = new stdClass();
        $objParsedQuery         = new stdClass();
        if(!stristr($uri, "?")){
            $objStripped->M     = $uri;
            $objStripped->Q     = null;
        }else{
            $arrSplit           = preg_split("#\?#", $uri);
            $objStripped->M     = $arrSplit[0];
            $objStripped->Q     = $arrSplit[1];
        }
        $cleanURL               = $objStripped->M;
        if($objStripped->Q){
            $arrSplit       = preg_split("#[\?\&]#", $objStripped->Q);
            if(!empty($arrSplit) && count($arrSplit)>0 ) {
                foreach ($arrSplit as $queryKVPair) {
                    preg_match("#(.*)(\=)(.*)#", $queryKVPair, $matches);
                    list($fullNull, $key, $null, $value) = $matches;
                    $objParsedQuery->$key = $value;
                    $cleanURL  .=  "/" . $value;
                }
            }
            $objStripped->Q = $objParsedQuery;
        }
        return $cleanURL;
    }

【讨论】:

    猜你喜欢
    • 2020-08-10
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2022-11-17
    • 2023-04-10
    • 2010-11-13
    相关资源
    最近更新 更多