请求PHP中没有GET参数的字符串 [英] Request string without GET arguments in PHP
问题描述
有没有一种简单的方法来获取没有GET参数的请求文件或目录?例如,如果URL是 http://mysite.com/directory/file.php?paramater=value
我想只返回 http ://mysite.com/directory/file.php
。我很惊讶在 $ _ SERVER []
中没有简单的索引。我错过了吗?谢谢!
Is there a simple way to get the requested file or directory without the GET arguments? For example, if the URL is http://mysite.com/directory/file.php?paramater=value
I would like to return just http://mysite.com/directory/file.php
. I was surprised that there is not a simple index in $_SERVER[]
. Did I miss one? Thanks!
推荐答案
您可以使用 $ _ SERVER ['REQUEST_URI']
获取请求的路径。然后,你需要删除参数......
You can use $_SERVER['REQUEST_URI']
to get requested path. Then, you'll need to remove the parameters...
$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
然后,添加主机名和协议。
Then, add in the hostname and protocol.
echo 'http://' . $_SERVER['HTTP_HOST'] . $uri_parts[0];
请注意,如果混合,您还必须检测协议http:
和 https://
。我作为练习留给你。
Note that you'll have to detect protocol as well, if you mix http:
and https://
. That I leave as an exercise for you.
这篇关于请求PHP中没有GET参数的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!