使用保持活动连接 (HTTP 1.1) 在 PHP 中创建类似 ftp 的会话
我正在寻找一种方法来跟踪跨请求保持活动的 HTTP 1.1 连接,以获得类似 ftp 的会话。 这个想法是在第一次请求时进行身份验证,然后在套接字仍然打开时保持身份验证有效(使用 HTTP 1.1 保持活动功能)。
我一直在寻找这样的解决方案,但没有取得多大成功,直到这里。
我正在寻找信息,例如:
- PHP 中的 apache 是否有可用的套接字 ID?
- 是否有一个模块允许向 HTTP 1.1 连接添加信息(可以从 PHP 中使用)?
还有其他想法吗?
I'm looking for a way to keep track of a HTTP 1.1 connection kept alive across requests, in order to get a ftp-like session.
The idea would be to have an authentication at first request, and then, keeping the authentication valid while the socket is still open (with HTTP 1.1 keep-alive feature).
I've been searching for such a solution without much success until here.
I'm looking for information, such as:
- is there somewhere a socket ID available from apache in PHP?
- is there a module allowing to add information to an HTTP 1.1 connection (something that could be used from PHP)?
Any other idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能那样做。
我不认为 Apache(或任何 Web 服务器)向 PHP 脚本公开套接字 ID。但使用
$_SERVER['REMOTE_ADDR']
和$_SERVER['REMOTE_PORT']
可以唯一标识一个连接,因为同一客户端无法发起两个连接同时从同一临时端口到同一服务器。但即使您这样做,保持活动会话也可能不会停留足够长的时间,无法像 FTP 那样进行有意义的人机交互。大多数保持活动会话会在 5-15 秒后自动终止,具体取决于您的 Web 服务器的配置。您也许可以对此进行调整,但是让连接保持活动时间超过这个时间是一个非常糟糕的主意,特别是在将 Apache 与 mod_php 一起使用时,因为持久的连接可能会以牺牲其他用户为代价来独占服务器资源。此外,计算机和路由器往往会重复使用临时端口,因此无法保证您正在与同一用户通信。当您考虑到人们每天使用的所有代理和反向代理时,事情会变得更加复杂。
只需使用会话和/或 cookie。他们总是工作。
You can't do that.
I don't think Apache (or any web server, for that matter) exposes socket IDs to a PHP script. But it may be possible to uniquely identify a connection using
$_SERVER['REMOTE_ADDR']
and$_SERVER['REMOTE_PORT']
because the same client can't initiate two connections to the same server from the same ephemeral port at the same time.But even if you do this, the keep-alive session probably won't stay around long enough to allow for meaningful human interaction like FTP does. Most keep-alive sessions are automatically terminated after 5-15 seconds, depending on your web server's configuration. You may be able to tweak this, but it's a very bad idea to keep connections alive for longer than that, especially when using Apache with mod_php, because a long-lasting connection can monopolize server resources at the expense of other users. Besides, computers and routers tend to reuse ephemeral ports, so there's no guarantee that you're talking to the same user. Things get even more complicated when you take into account all the proxies and reverse proxies that people use every day.
Just use sessions and/or cookies. They always work.
如果你不限于apache,并且可以简单地运行php脚本,我认为你可以做到。
http://php.net/manual/en/book.sockets.php
PHP 允许您打开特定端口并对其进行低级读写。这样您就可以编写任何现有的或您自己的协议。
对于这种特殊情况,您可以让 apache 在非 http 端口上运行,并从自定义 PHP 脚本监听 http 端口。然后,您应验证连接请求;为用户附加一个自定义标头(称为 myauth),然后将请求转发到 apache。确保在读取原始 http 请求时过滤掉 myauth 标头。
If you are not limited to apache, and can simply run php scripts I think you can do it.
http://php.net/manual/en/book.sockets.php
PHP allows you to open a specific port and have low level read writes on it. This way you can write any existing or your own protocols.
For this particular situation, you can have apache running on a non-http port and listen the http port from custom PHP script. You shall then authenticate the request on connection; append a custom header(call it myauth) for user and then forward the request to apache. Make sure you filter out the myauth header while reading the original http request.