{"id":982,"date":"2019-08-21T15:34:47","date_gmt":"2019-08-21T07:34:47","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=982"},"modified":"2023-12-04T16:42:22","modified_gmt":"2023-12-04T08:42:22","slug":"codeigniter6-%e5%9c%b0%e5%9d%80%e8%a7%a3%e6%9e%90%e7%b1%bburi-php","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=982","title":{"rendered":"CodeIgniter(6)\u2014\u2014\u5730\u5740\u89e3\u6790\u7c7bURI.php"},"content":{"rendered":"<p>URI\u7c7b\u7684\u4f5c\u7528\u4e3b\u8981\u662f\u5904\u7406\u5730\u5740\u5b57\u7b26\u4e32\uff0c\u5c06URI\u5206\u6210\u5bf9\u5e94\u7684\u7247\u6bb5\u4fdd\u5b58\u5230segments\uff0c\u8def\u7531\u7c7b\u4e5f\u4e3b\u8981\u662f\u901a\u8fc7segments\u6570\u7ec4\u6765\u83b7\u53d6\u4e0a\u4e0b\u6587\u4e2d\u7684URI\u8bf7\u6c42\u4fe1\u606f \u3002<br \/>\n1. URI\u7c7b\u662f\u5982\u4f55\u5c06\u5730\u5740\u5b57\u7b26\u4e32\u89e3\u6790\u6210\u5bf9\u5e94\u7247\u6bb5\uff1f<br \/>\n\u7b54\uff1aURI\u7c7b\u9996\u5148\u5c06URL\u5b57\u7b26\u4e32\u89e3\u6790\u6210URI\u5b57\u7b26\u4e32\uff0cURI\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5219\u662f\u6211\u4eec\u5df2\u7ecf\u975e\u5e38\u719f\u6089\u7684CI\u8def\u7531\u5730\u5740\uff08\u67e5\u8be2\u5b57\u7b26\u4e32\uff0cSCRIPT_NAME\uff0c\u4ee5\u53caSCRIPT_NAME\u76ee\u5f55\u540d\u4e0d\u4f1a\u51fa\u73b0\u5728uri\u5b57\u7b26\u4e32\u4e2d\uff09\uff0c\u7136\u540e\u518d\u5c06URI\u5b57\u7b26\u4e32\u4e2d\u7684\u53c2\u6570\u89e3\u6790\u51fa\u6765\u5b58\u50a8\u5230segments\u6570\u7ec4\u4e2d\uff0c\u8fd9\u6837\u5c31\u8bb2url\u89e3\u6790\u6210\u5bf9\u5e94\u7684\u7247\u6bb5\u4e86\u3002<br \/>\n2. \u89e3\u6790\u540e\u7684\u5bf9\u5e94\u7247\u6bb5\u4fdd\u5b58\u5230\u53d8\u91cf\u4e2d\u662f\u600e\u6837\u7684\u6570\u636e\u7ed3\u6784\uff1f<br \/>\n\u7b54\uff1a\u89e3\u6790\u51fa\u6765\u7684\u7247\u6bb5\u4ece\u4e0b\u68071\u5f00\u59cb\u4f9d\u6b21\u4fdd\u5b58\u5230segments\u6570\u7ec4\u4e2d\u3002<\/p>\n<h3>1.__construct()\u6784\u9020\u51fd\u6570<\/h3>\n<p>URI\u7c7b\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u5c31\u4f1a\u5bf9\u5730\u5740\u8fdb\u884c\u89e3\u6790\uff0c\u6784\u9020\u51fd\u6570\u4f1a\u6839\u636e\u4e0d\u540c\u7684\u73af\u5883\u8c03\u7528\u5bf9\u5e94\u7684\u89e3\u6790\u51fd\u6570\uff0c\u5e76\u4fdd\u5b58\u89e3\u6790\u7ed3\u679c\u3002 cli\u6a21\u5f0f\uff1a\u8c03\u7528_parse_argv()\u8fdb\u884c\u89e3\u6790 \u6839\u636euri_protocol\u8fd9\u4e2a\u914d\u7f6e\u5c5e\u6027\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e2a\u89e3\u6790\u51fd\u6570 \u9ed8\u8ba4REQUEST_URI\uff0c\u4f7f\u7528_parse_request_uri\u89e3\u6790\u51fd\u6570 QUERY_STRING\uff0c\u4f7f\u7528_parse_query_string\u89e3\u6790\u51fd\u6570 PATH_INFO\u6216\u5176\u4ed6\u53c2\u6570\uff0c\u90fd\u4f7f\u7528_parse_request_uri\u89e3\u6790\u51fd\u6570 \u8fd9\u4e9b\u89e3\u6790\u51fd\u6570\u4f1a\u5c06\u5730\u5740\u89e3\u6790\u6210uri\u5b57\u7b26\u4e32\uff0c\u518d\u7531_set_uri_string\u51fd\u6570\u5c06uri\u5b57\u7b26\u4e32\u89e3\u6790\u6210\u5bf9\u5e94\u7247\u6bb5<\/p>\n<pre><code class=\"language-php line-numbers\">public function __construct()\n    {\n        $this-&gt;config =&amp; load_class('Config', 'core');\n\n        \/\/ If query strings are enabled, we don't need to parse any segments.\n        \/\/ However, they don't make sense under CLI.\n        if (is_cli() OR $this-&gt;config-&gt;item('enable_query_strings') !== TRUE)\n        {\n            $this-&gt;_permitted_uri_chars = $this-&gt;config-&gt;item('permitted_uri_chars');\n\n            \/\/ If it's a CLI request, ignore the configuration\n            if (is_cli())\n            {\n                $uri = $this-&gt;_parse_argv();\n            }\n            else\n            {\n                $protocol = $this-&gt;config-&gt;item('uri_protocol');\n                empty($protocol) &amp;&amp; $protocol = 'REQUEST_URI';\n\n                switch ($protocol)\n                {\n                    case 'AUTO': \/\/ For BC purposes only\n                    case 'REQUEST_URI':\n                        $uri = $this-&gt;_parse_request_uri();\n                        break;\n                    case 'QUERY_STRING':\n                        $uri = $this-&gt;_parse_query_string();\n                        break;\n                    case 'PATH_INFO':\n                    default:\n                        $uri = isset($_SERVER[$protocol])\n                            ? $_SERVER[$protocol]\n                            : $this-&gt;_parse_request_uri();\n                        break;\n                }\n            }\n\n            $this-&gt;_set_uri_string($uri);\n        }\n\n        log_message('info', 'URI Class Initialized');\n    }\n<\/code><\/pre>\n<h3>2.\u547d\u4ee4\u884c\u6a21\u5f0f\u4e0b_parse_argv()\u89e3\u6790\u51fd\u6570<\/h3>\n<p>\u8fd9\u4e2auri\u5b57\u7b26\u4e32\u89e3\u6790\u6570\u7ec4\uff0c\u662f\u4ece$_SERVER[&#8216;argv&#8217;]\u4e2d\u83b7\u53d6\u53c2\u6570<\/p>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Parse CLI arguments\n     *\n     * Take each command line argument and assume it is a URI segment.\n     *\n     * @return  string\n     *\/\n    protected function _parse_argv()\n    {\n        $args = array_slice($_SERVER['argv'], 1);\n        return $args ? implode('\/', $args) : '';\n    }\n<\/code><\/pre>\n<h3>3._parse_request_uri()\u89e3\u6790\u51fd\u6570<\/h3>\n<p>\u4ece$_SERVER[&#8216;REQUEST_URI&#8217;]\u4e2d\u83b7\u53d6\u53c2\u6570<\/p>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Parse REQUEST_URI\n     *\n     * Will parse REQUEST_URI and automatically detect the URI from it,\n     * while fixing the query string if necessary.\n     *\n     * @return  string\n     *\/\n    protected function _parse_request_uri()\n    {\n        if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))\n        {\n            return '';\n        }\n\n        \/\/ parse_url() returns false if no host is present, but the path or query string\n        \/\/ contains a colon followed by a number\n        $uri = parse_url('http:\/\/dummy'.$_SERVER['REQUEST_URI']);\n        $query = isset($uri['query']) ? $uri['query'] : '';\n        $uri = isset($uri['path']) ? $uri['path'] : '';\n\n        if (isset($_SERVER['SCRIPT_NAME'][0]))\n        {\n            if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)\n            {\n                $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));\n            }\n            elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)\n            {\n                $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));\n            }\n        }\n\n        \/\/ This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct\n        \/\/ URI is found, and also fixes the QUERY_STRING server var and $_GET array.\n        if (trim($uri, '\/') === '' &amp;&amp; strncmp($query, '\/', 1) === 0)\n        {\n            $query = explode('?', $query, 2);\n            $uri = $query[0];\n            $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';\n        }\n        else\n        {\n            $_SERVER['QUERY_STRING'] = $query;\n        }\n\n        parse_str($_SERVER['QUERY_STRING'], $_GET);\n\n        if ($uri === '\/' OR $uri === '')\n        {\n            return '\/';\n        }\n\n        \/\/ Do some final cleaning of the URI and return it\n        return $this-&gt;_remove_relative_directory($uri);\n    }\n<\/code><\/pre>\n<h3>4._parse_query_string()\u89e3\u6790\u51fd\u6570<\/h3>\n<p>\u6839\u636e$_SERVER[&#8216;QUERY_STRING&#8217;]\u7684\u53c2\u6570\u89e3\u6790\u51fa\u5b57\u7b26\u4e32<\/p>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Parse QUERY_STRING\n     *\n     * Will parse QUERY_STRING and automatically detect the URI from it.\n     *\n     * @return  string\n     *\/\n    protected function _parse_query_string()\n    {\n        $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');\n\n        if (trim($uri, '\/') === '')\n        {\n            return '';\n        }\n        elseif (strncmp($uri, '\/', 1) === 0)\n        {\n            $uri = explode('?', $uri, 2);\n            $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';\n            $uri = $uri[0];\n        }\n\n        parse_str($_SERVER['QUERY_STRING'], $_GET);\n\n        return $this-&gt;_remove_relative_directory($uri);\n    }\n<\/code><\/pre>\n<h3>5._remove_relative_directory()\u2014\u2014\u53bb\u6389\u591a\u4f59\u659c\u6760\u548c\u76f8\u5bf9\u8def\u5f84\u7b26\u53f7<\/h3>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Remove relative directory (..\/) and multi slashes (\/\/\/)\n     *\n     * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()\n     *\n     * @param   string  $uri\n     * @return  string\n     *\/\n    protected function _remove_relative_directory($uri)\n    {\n        $uris = array();\n        $tok = strtok($uri, '\/');\n        while ($tok !== FALSE)\n        {\n            if (( ! empty($tok) OR $tok === '0') &amp;&amp; $tok !== '..')\n            {\n                $uris[] = $tok;\n            }\n            $tok = strtok('\/');\n        }\n\n        return implode('\/', $uris);\n    }\n<\/code><\/pre>\n<h3>6.uri\u89e3\u6790\u6210\u5bf9\u5e94\u5206\u6bb5\u2014\u2014_set_uri_string()\u51fd\u6570<\/h3>\n<p>\u524d\u9762\u89e3\u8bfb\u7684\u51e0\u4e2a\u51fd\u6570\u5df2\u7ecf\u5c06url\u89e3\u6790\u6210uri\u5b57\u7b26\u4e32\u4e86\uff0c\u4f46\u662f\u6211\u4eec\u6700\u7ec8\u9700\u8981\u7684\u662fUri\u5b57\u7b26\u4e32\u6240\u5bf9\u5e94\u7684\u53c2\u6570\uff0c\u8fd9\u6837\u624d\u80fd\u6839\u636euri\u53c2\u6570\u8def\u7531\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\uff0cset_uri_string()\u51fd\u6570\u7684\u529f\u80fd\u4fbf\u662f\u5c06uri\u5b57\u7b26\u4e32\u89e3\u6790\u6210\u5bf9\u5e94\u5206\u6bb5<\/p>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Set URI String\n     *\n     * @param   string  $str\n     * @return  void\n     *\/\n    protected function _set_uri_string($str)\n    {\n        \/\/ Filter out control characters and trim slashes\n        $this-&gt;uri_string = trim(remove_invisible_characters($str, FALSE), '\/');\n\n        if ($this-&gt;uri_string !== '')\n        {\n            \/\/ Remove the URL suffix, if present\n            if (($suffix = (string) $this-&gt;config-&gt;item('url_suffix')) !== '')\n            {\n                $slen = strlen($suffix);\n\n                if (substr($this-&gt;uri_string, -$slen) === $suffix)\n                {\n                    $this-&gt;uri_string = substr($this-&gt;uri_string, 0, -$slen);\n                }\n            }\n\n            $this-&gt;segments[0] = NULL;\n            \/\/ Populate the segments array\n            foreach (explode('\/', trim($this-&gt;uri_string, '\/')) as $val)\n            {\n                $val = trim($val);\n                \/\/ Filter segments for security\n                $this-&gt;filter_uri($val);\n\n                if ($val !== '')\n                {\n                    $this-&gt;segments[] = $val;\n                }\n            }\n\n            unset($this-&gt;segments[0]);\n        }\n    }\n<\/code><\/pre>\n<h3>7.\u5408\u6cd5\u6027\u4fdd\u969c\u2014\u2014filter_uri\u51fd\u6570<\/h3>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Filter URI\n     *\n     * Filters segments for malicious characters.\n     *\n     * @param   string  $str\n     * @return  void\n     *\/\n    public function filter_uri(&amp;$str)\n    {\n        if ( ! empty($str) &amp;&amp; ! empty($this-&gt;_permitted_uri_chars) &amp;&amp; ! preg_match('\/^['.$this-&gt;_permitted_uri_chars.']+$\/i'.(UTF8_ENABLED ? 'u' : ''), $str))\n        {\n            show_error('The URI you submitted has disallowed characters.', 400);\n        }\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>URI\u7c7b\u7684\u4f5c\u7528\u4e3b\u8981\u662f\u5904\u7406\u5730\u5740\u5b57\u7b26\u4e32\uff0c\u5c06URI\u5206\u6210\u5bf9\u5e94\u7684\u7247\u6bb5\u4fdd\u5b58\u5230segments\uff0c\u8def\u7531\u7c7b\u4e5f\u4e3b\u8981\u662f\u901a\u8fc7segme [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[405],"class_list":["post-982","post","type-post","status-publish","format-standard","hentry","category-15","tag-php"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=982"}],"version-history":[{"count":2,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":984,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/982\/revisions\/984"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}