PHP 中是否有像 ASP.Net 中那样的 httphandlers
在 ASP.Net 中,我们有 Httphandlers,它可以解析请求并相应地重定向用户,大多数情况下我们将它们用于 url 重写等。
我想知道我们在 PHP 中是否也有这样的功能,如果有,请提供一个解释它的示例将会非常有帮助。
例如,如果我在某个服务器上请求图像文件,如果请求来自服务器 x,我想将其重定向到某个页面
In ASP.Net we have Httphandlers which can parse a request and accordingly redirect the user, mostly we use them for url-rewriting etc.
I would like to know do we have such functionality in PHP as well, if yes a example which explains it will be very helpful.
For instance if I am requesting a image file on some server , I would like to redirect it to some page if the request if from server x
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解 HTTPHandlers,这通常是通过一些技巧来完成的。显然,任何传入 Web 服务器(IIS、Apache 等)的请求都会尝试与文件匹配。
该请求将到达
index.php
文件,PHP 将处理该文件,然后将响应发送到客户端。显然,客户端将看到 PHP 产生的任何输出。HTTPHandler 的工作方式是查看传入请求并将该请求映射到一段代码(可能是一个函数),但它可以告诉 Web 服务器客户端应如何通过配置访问该代码。
以下是借自 Microsoft 的示例,它说明了映射。
在 PHP 中,这可以通过两步过程完成。首先,Web 服务器需要将所有请求路由到单个 PHP 文件。其次,该 PHP 文件必须指导程序流程。
在 Apache 中,您可以使用网站根目录中的
.htaccess
文件配置路由位。这个示例将路由任何不存在的内容磁盘到index.php
。从那里,PHP 只需要知道用户正在寻找什么 url,并决定要做什么。这可以通过使用从 Apache 传递的
REQUEST_URI
值来完成。因此,如果用户请求:
REQUEST_URI
将是:除此之外,人们可以将正则表达式应用于
REQUEST_URI
并在多个函数之间切换来处理请求。If I understand HTTPHandlers correctly, this is generally done by a bit of trickery. Obviously any request that comes in to a web server (IIS,Apache,etc.) is going to try and match to a file.
This request will reach the
index.php
file, PHP will process that file, and then the response will be sent to the client. Obviously the client will see whatever output PHP produces.HTTPHandlers work by seeing the incoming request and mapping that request to a segment of code (perhaps a function), but it has the luxury of telling the web server how a client should access that code via configuration.
The following is an example borrowed from microsoft which illustrates the mapping.
In PHP, this can be done in a two step process. First, the web server needs to route all requests to a single PHP file. And second, that PHP file must direct the program flow.
In Apache you can configure the routing bit with a
.htaccess
file in the root of your website. This example will route anything that doesn't exist on disk toindex.php
.From there, PHP just needs to know what url the user was looking for, and decide what to do. This can be done by using the
REQUEST_URI
value passed from Apache.So, if a user requests:
REQUEST_URI
would be:Beyond that, a person could apply a regex to
REQUEST_URI
and switch between a number of functions to handle the request.一些 php 框架提供类似的功能。如果你想在 php 中重定向,你可以直接设置 headers,
如果你想要当前的 url 等,你需要查询 $_SERVER var
试试这个,看看你可以从你的服务器获得什么信息
some of the php frameworks offer similar functionality. if you want to redirect in php you would set the headers directly
if you want the current url etc, you would need to query the $_SERVER var
try this, and see what information you can get from your server
您必须实现自己的机制或使用框架。 Zend Framework 的请求经历了类似的概念,但仍然与处理程序有很大不同。
You would have to implement your own mechanism or use a framework. Zend Framework's Requests go through a similar concept but still quite different than the handlers.