{"id":22764607,"url":"https://github.com/kuria/request-info","last_synced_at":"2025-03-30T10:14:14.634Z","repository":{"id":57009943,"uuid":"167571911","full_name":"kuria/request-info","owner":"kuria","description":"Get information about the current HTTP request","archived":false,"fork":false,"pushed_at":"2023-07-07T10:02:39.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-06T01:49:05.946Z","etag":null,"topics":["base-dir","base-path","forwarded","host","http","https","ip","path-info","php","port","request","trusted-proxies","url","x-forwarded"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kuria.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-25T15:49:03.000Z","updated_at":"2023-09-19T17:45:12.000Z","dependencies_parsed_at":"2025-02-05T12:12:05.473Z","dependency_job_id":"29d1a6f9-fe38-4a46-a630-ffe455499d8f","html_url":"https://github.com/kuria/request-info","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Frequest-info","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Frequest-info/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Frequest-info/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Frequest-info/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kuria","download_url":"https://codeload.github.com/kuria/request-info/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246301995,"owners_count":20755514,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["base-dir","base-path","forwarded","host","http","https","ip","path-info","php","port","request","trusted-proxies","url","x-forwarded"],"created_at":"2024-12-11T12:09:28.722Z","updated_at":"2025-03-30T10:14:14.614Z","avatar_url":"https://github.com/kuria.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Request info\n############\n\nGet information about the current HTTP request.\n\n.. image:: https://travis-ci.com/kuria/request-info.svg?branch=master\n   :target: https://travis-ci.com/kuria/request-info\n\n.. contents::\n   :depth: 3\n\n\nFeatures\n********\n\n- getting request information:\n\n  - headers\n  - HTTPS detection\n  - client IP address\n  - scheme\n  - method\n  - host\n  - port\n  - URL\n  - base directory\n  - base path\n  - path info\n  - script name\n\n- trusted proxy header support (x-forwarded / forwarded)\n- host validation (inc. defining specific trusted hosts or host patterns)\n- optional HTTP method override support\n\n\nRequirements\n************\n\n- PHP 7.1+\n\n\nUsage\n*****\n\nAll configuration and value retrieval is done via the static ``Kuria\\RequestInfo\\RequestInfo`` class.\n\n\nConfiguration\n=============\n\nTrusted proxies\n---------------\n\nBy default all proxy headers are ignored. To trust select proxy headers, call ``RequestInfo::setTrustedProxies()``\nwith an appropriately configured ``TrustedProxies`` instance.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n   use Kuria\\RequestInfo\\TrustedProxies;\n\n   $trustedProxies = new TrustedProxies(\n       ['192.168.1.10', '192.168.1.20'],  // one or more IP adresses or subnets in CIDR notation\n       TrustedProxies::HEADER_FORWARDED   // which headers to trust (bit mask)\n   );\n\n   RequestInfo::setTrustedProxies($trustedProxies);\n\n\nChoosing which headers to trust\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nTrusted headers are a bitmask of the following constants:\n\n============================================== ==============================\nConstant                                       Allowed headers\n============================================== ==============================\n``TrustedProxies::HEADER_FORWARDED``           ``Forwarded``\n``TrustedProxies::HEADER_X_FORWARDED_FOR``     ``X-Forwarded-For``\n``TrustedProxies::HEADER_X_FORWARDED_HOST``    ``X-Forwarded-Host``\n``TrustedProxies::HEADER_X_FORWARDED_PROTO``   ``X-Forwarded-Proto``\n``TrustedProxies::HEADER_X_FORWARDED_PORT``    ``X-Forwarded-Port``\n``TrustedProxies::HEADER_X_FORWARDED_ALL``     ``X-Forwarded-*``\n============================================== ==============================\n\n.. NOTE::\n\n   Trusting both the ``Forwarded`` and ``X-Forwarded-*`` headers is supported,\n   but they must report the same values. Different values will cause\n   ``Kuria\\RequestInfo\\Exception\\HeaderConflictException``.\n\n\nApplications always behind a trusted proxy\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nIf you are sure that an application will always be behind a trusted proxy, you can\nuse ``$_SERVER['REMOTE_ADDR']`` in place of a hardcoded IP address:\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n   use Kuria\\RequestInfo\\Helper\\Server;\n   use Kuria\\RequestInfo\\TrustedProxies;\n\n   $trustedProxies = new TrustedProxies(\n       [Server::require('REMOTE_ADDR')],\n       TrustedProxies::HEADER_FORWARDED\n   );\n\n   RequestInfo::setTrustedProxies($trustedProxies);\n\n\nTrusted hosts\n-------------\n\nThe request host is always validated according to the standards.\n\nTo restrict accepted hosts further, use the following methods:\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   // specific hosts (exact match)\n   RequestInfo::setTrustedHosts([\n       'www.example.com',\n       'cdn.example.com',\n   ]);\n\n   // host patterns\n   RequestInfo::setTrustedHostPatterns([\n       '{\\w+\\.example\\.com$}AD',\n       '{example-node-\\d+$}AD',\n   ]);\n\n\nHTTP method override\n--------------------\n\nBy default, the ``X-HTTP-Method-Override`` header is ignored.\n\nIf you need to override the HTTP method via this header (e.g. because of restrictive firewall rules),\nyou can enable its support:\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   RequestInfo::setAllowHttpMethodOverride(true);\n\n\nResetting configuration\n-----------------------\n\nTo restore default ``RequestInfo`` configuration:\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   RequestInfo::reset();\n\n\nGetting request information\n===========================\n\nHeaders\n-------\n\nGet all request headers as an array. Header names are lowercased and used as keys.\n\n.. code:: php\n\n   \u003c?php\n\n   print_r(RequestInfo::getHeaders());\n\nExample output:\n\n::\n\n  Array\n  (\n      [host] =\u003e localhost:8080\n      [connection] =\u003e keep-alive\n      [cache-control] =\u003e max-age=0\n      [upgrade-insecure-requests] =\u003e 1\n      [user-agent] =\u003e Mozilla/5.0 (Example)\n      [accept] =\u003e text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\n      [accept-encoding] =\u003e gzip, deflate, br\n      [accept-language] =\u003e en-US,en;q=0.9,cs;q=0.8\n  )\n\n\nTrusted proxy detection\n-----------------------\n\nCheck whether the request originated from a trusted proxy.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   if (RequestInfo::isFromTrustedProxy()) {\n       // request is from a trusted proxy\n   }\n\n\nHTTPS detection\n---------------\n\nSee whether the request uses HTTPS.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   if (RequestInfo::isSecure()) {\n       // request uses HTTPS\n   }\n\n\nClient IP address\n-----------------\n\nGet the client IP address.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getClientIp());\n\nExample output:\n\n::\n\n  string(9) \"127.0.0.1\"\n\n.. NOTE::\n\n   ``RequestInfo::getClientIp()`` will return ``NULL`` if the client IP address is not known (e.g. in CLI).\n\nTo get all known client IP addresses (ordered from most trusted to least trusted), use ``getClientIps()``:\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   print_r(RequestInfo::getClientIps());\n\nExample output:\n\n::\n\n  Array\n  (\n      [0] =\u003e 20.30.40.50\n      [1] =\u003e 10.20.30.40\n  )\n\n.. NOTE::\n\n   ``RequestInfo::getClientIps()`` will return an empty array if the client IP addresses are not known (e.g. in CLI).\n\n\nMethod\n------\n\nGet the request method. The method name will always be in uppercase.\n\nAlso see `HTTP method override`_.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getMethod());\n\nExample output:\n\n::\n\n  string(3) \"GET\"\n\n\n\nScheme\n------\n\nGet the request scheme.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getScheme());\n\nExample output:\n\n::\n\n  string(4) \"https\"\n\n\nHost\n----\n\nGet the host name.\n\nAlso see `Trusted hosts`_.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getHost());\n\nExample output:\n\n::\n\n  string(9) \"localhost\"\n\n.. NOTE::\n\n   The returned host name does not include the port number. Use ``RequestInfo::getPort()`` to get\n   the port number or ``RequestInfo::getUrl()-\u003egetFullHost()`` to get the host name with the port\n   number (if it is non-standard).\n\n\nPort\n----\n\nGet the port number.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getPort());\n\nExample output:\n\n::\n\n  int(80)\n\n\nURL\n---\n\nGet the request URL. Returns an unique instance of ``Kuria\\Url\\Url``.\n\nSee the `kuria/url \u003chttps://github.com/kuria/url\u003e`_ component for more information.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   $url = RequestInfo::getUrl();\n\n   echo\n       \"URL:\\t\", $url-\u003ebuild(), PHP_EOL,\n       \"Scheme:\\t\", $url-\u003egetScheme(), PHP_EOL,\n       \"Host:\\t\", $url-\u003egetHost(), PHP_EOL,\n       \"Port:\\t\", $url-\u003egetPort(), PHP_EOL,\n       \"Path:\\t\", $url-\u003egetPath(), PHP_EOL,\n       \"Query:\\t\", json_encode($url-\u003egetQuery()), PHP_EOL;\n\nExample output:\n\n::\n\n  URL:    http://localhost:8080/test/index.php/foo?bar=baz\n  Scheme: http\n  Host:   localhost\n  Port:   8080\n  Path:   /test/index.php/foo\n  Query:  {\"bar\":\"baz\"}\n\n\nBase directory\n--------------\n\nGet base directory (without script name, if any). The returned path never ends with a \"/\".\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getBaseDir());\n\nExamples:\n\n================================= ===============\nURL                               Base directory\n================================= ===============\nhttp://localhost/index.php        *(empty string)*\nhttp://localhost/index.php/page   *(empty string)*\nhttp://localhost/web/index.php    /web\nhttp://localhost/we%20b/index.php /we%20b\n================================= ===============\n\n\nBase path\n---------\n\nGet base path (including the script name, if any). The returned path never ends with a \"/\".\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getBasePath());\n\nExamples:\n\n================================= =================\nURL                               Base path\n================================= =================\nhttp://localhost/index.php        /index.php\nhttp://localhost/index.php/page   /index.php\nhttp://localhost/web/index.php    /web/index.php\nhttp://localhost/we%20b/index.php /we%20b/index.php\n================================= =================\n\n\nPath info\n---------\n\nGet path info.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getPathInfo());\n\nExamples:\n\n=========================================== =================\nURL                                         Path info\n=========================================== =================\nhttp://localhost/index.php                  *(empty string)*\nhttp://localhost/index.php/page             /page\nhttp://localhost/web/index.php              *(empty string)*\nhttp://localhost/we%20b/index.php/foo%20bar /foo%20bar\n=========================================== =================\n\n\nScript name\n-----------\n\nGet the current script name.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\RequestInfo\\RequestInfo;\n\n   var_dump(RequestInfo::getScriptName());\n\nExample output:\n\n::\n\n  string(18) \"./public/index.php\"\n\n\nInternal cache\n==============\n\nMost methods of the ``RequestInfo`` class cache their results internally. If you manipulate ``$_SERVER``\nafter already reading some request information, you will need to call ``RequestInfo::clearCache()``\nto clear the cache.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuria%2Frequest-info","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkuria%2Frequest-info","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuria%2Frequest-info/lists"}