请求不带GET参数的字符串 [英] Request string without GET arguments

查看:217
本文介绍了请求不带GET参数的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来获取没有GET参数的请求的文件或目录?例如,如果URL是http://example.com/directory/file.php?paramater=value,我只想返回http://example.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://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?

推荐答案

您可以使用$_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://,则还必须检测协议. 我留给你做练习. $_SERVER['REQUEST_SCHEME']返回协议.

You'll have to detect protocol as well, if you mix http: and https://. That I leave as an exercise for you. $_SERVER['REQUEST_SCHEME'] returns the protocol.

将它们放在一起:

echo $_SERVER['REQUEST_SCHEME'] .'://'. $_SERVER['HTTP_HOST'] 
     . explode('?', $_SERVER['REQUEST_URI'], 2)[0];

...返回,例如:

http://example.com/directory/file.php


php.com文档:

  • $_SERVER —服务器和执行环境信息
  • explode -用字符串将字符串分隔
  • parse_url —解析URL并返回其URL组件(可能是更好的解决方案)

  • php.com Documentation:

    • $_SERVER — Server and execution environment information
    • explode — Split a string by a string
    • parse_url — Parse a URL and return its components (possibly a better solution)
    • 这篇关于请求不带GET参数的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆