Is there a simple way to get the requested file or directory without the GET arguments? For example, if the URL is http://example.com/directory/file.php?paramater=value
I would like to return just http://example.com/directory/file.php
. I was surprised that there is not a simple index in $_SERVER[]
. Did I miss one?
相关问题
- Views base64 encoded blob in HTML with PHP
- Angular RxJS mergeMap types
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
Edit: @T.Todua provided a newer answer to this question using parse_url.
(please upvote that answer so it can be more visible).
Edit2: Someone has been spamming and editing about extracting scheme, so I've added that at the bottom.
parse_url solution
Parse_url is a built-in php function, who's sole purpose is to extract specific components from a url, including the
PATH
(everything before the first?
). As such, it is my new "best" solution to this problem.strtok solution
Stackoverflow: How to remove the querystring and get only the url?
Performance Note: This problem can also be solved using explode.
This application of strtok to return everything in a string before the first instance of a character will perform better than any other method in PHP, though WILL leave the querystring in memory.
An aside about Scheme (http/https) and
$_SERVER
varsWhile OP did not ask about it, I suppose it is worth mentioning: parse_url should be used to extract any specific component from the url, please see the documentation for that function:
Of note here, is that getting the full URL from a request is not a trivial task, and has many security implications.
$_SERVER
variables are your friend here, but they're a fickle friend, as apache/nginx configs, php environments, and even clients, can omit or alter these variables. All of this is well out of scope for this question, but it has been thoroughly discussed:https://stackoverflow.com/a/6768831/1589379
It is important to note that these
$_SERVER
variables are populated at runtime, by whichever engine is doing the execution (/var/run/php/
or/etc/php/[version]/fpm/
). These variables are passed from the OS, to the webserver (apache/nginx) to the php engine, and are modified and amended at each step. The only such variables that can be relied on areREQUEST_URI
(because it's required by php), and those listed in RFC 3875 (see: PHP: $_SERVER ) because they are required of webservers.please note: spaming links to your answers across other questions is not in good taste.
It's shocking how many of these upvoted/accepted answers are incomplete, so they don't answer the OP's question, after 7 years!
If you are on a page with URL like:
http://example.com/directory/file.php?paramater=value
...and you would like to return just:
http://example.com/directory/file.php
then use:
Here is a solution that takes into account different ports and https:
Or a more basic solution that does not take other ports into account:
You can use
$_SERVER['REQUEST_URI']
to get requested path. Then, you'll need to remove the parameters...Then, add in the hostname and protocol.
You'll have to detect protocol as well, if you mix
http:
andhttps://
.That I leave as an exercise for you.$_SERVER['REQUEST_SCHEME']
returns the protocol.php.com Documentation:
$_SERVER
— Server and execution environment informationexplode
— Split a string by a stringparse_url
— Parse a URL and return its components (possibly a better solution)You can use
$_GET
for url params, or$_POST
for post params, but the$_REQUEST
contains the parameters from$_GET
$_POST
and$_COOKIE
, if you want to hide the URI parameter from the user you can convert it to a session variable like so:EDIT
use
$_SERVER['PHP_SELF']
to get the current file name or$_SERVER['REQUEST_URI']
to get the requested URI