PHP 中是否有像 ASP.Net 中那样的 httphandlers

发布于 2024-11-04 22:08:55 字数 183 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

行雁书 2024-11-11 22:08:55

如果我正确理解 HTTPHandlers,这通常是通过一些技巧来完成的。显然,任何传入 Web 服务器(IIS、Apache 等)的请求都会尝试与文件匹配。

GET /index.php   HTTP 1.1

该请求将到达 index.php 文件,PHP 将处理该文件,然后将响应发送到客户端。显然,客户端将看到 PHP 产生的任何输出。

HTTPHandler 的工作方式是查看传入请求并将该请求映射到一段代码(可能是一个函数),但它可以告诉 Web 服务器客户端应如何通过配置访问该代码。

以下是借自 Microsoft 的示例,它说明了映射。

<httpHandlers>
     <add verb="*" path="*.sync" type="MyHandler.SyncHandler, MyHandler" />
</httpHandlers>

在 PHP 中,这可以通过两步过程完成。首先,Web 服务器需要将所有请求路由到单个 PHP 文件。其次,该 PHP 文件必须指导程序流程。

在 Apache 中,您可以使用网站根目录中的 .htaccess 文件配置路由位。这个示例将路由任何不存在的内容磁盘到index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

从那里,PHP 只需要知道用户正在寻找什么 url,并决定要做什么。这可以通过使用从 Apache 传递的 REQUEST_URI 值来完成。

$_SERVER['REQUEST_URI'];

因此,如果用户请求:

GET /profile/settings  HTTP 1.1

REQUEST_URI 将是:

$url = $_SERVER['REQUEST_URI'];
echo $url; // => "/index.php/profile/settings"

除此之外,人们可以将正则表达式应用于 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.

GET /index.php   HTTP 1.1

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.

<httpHandlers>
     <add verb="*" path="*.sync" type="MyHandler.SyncHandler, MyHandler" />
</httpHandlers>

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 to index.php.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

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.

$_SERVER['REQUEST_URI'];

So, if a user requests:

GET /profile/settings  HTTP 1.1

REQUEST_URI would be:

$url = $_SERVER['REQUEST_URI'];
echo $url; // => "/index.php/profile/settings"

Beyond that, a person could apply a regex to REQUEST_URI and switch between a number of functions to handle the request.

浅唱々樱花落 2024-11-11 22:08:55

一些 php 框架提供类似的功能。如果你想在 php 中重定向,你可以直接设置 headers,

eg
header('Location: http://www.invalid.com/newpage.pgp');
exit; // make sure to exit or the script will keep executing

如果你想要当前的 url 等,你需要查询 $_SERVER var

试试这个,看看你可以从你的服务器获得什么信息

var_dump($_SERVER);

some of the php frameworks offer similar functionality. if you want to redirect in php you would set the headers directly

eg
header('Location: http://www.invalid.com/newpage.pgp');
exit; // make sure to exit or the script will keep executing

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

var_dump($_SERVER);
萌辣 2024-11-11 22:08:55

您必须实现自己的机制或使用框架。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文