简体   繁体   English

使用保持活动连接(HTTP 1.1)在PHP中创建类似ftp的会话

[英]Using keep-alive connections (HTTP 1.1) to create ftp-like sessions in 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. 我正在寻找一种跟踪HTTP 1.1连接并在请求中保持活动状态的方法,以便获得类似ftp的会话。 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). 这个想法将是在第一个请求时进行身份验证,然后在套接字仍处于打开状态时保持身份验证有效(具有HTTP 1.1保持活动功能)。

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? 在PHP中可以从Apache那里获得套接字ID吗?
  • is there a module allowing to add information to an HTTP 1.1 connection (something that could be used from PHP)? 是否有一个模块可以向HTTP 1.1连接添加信息(可以在PHP中使用)?

Any other idea? 还有其他想法吗?

You can't do that. 你不能那样做。

I don't think Apache (or any web server, for that matter) exposes socket IDs to a PHP script. 我不认为Apache(或其他任何Web服务器)将套接字ID公开给PHP脚本。 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. 但是使用$_SERVER['REMOTE_ADDR']$_SERVER['REMOTE_PORT']唯一标识连接是可能的,因为同一客户端无法同时从同一临时端口启动到同一服务器的两个连接。

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. 但是,即使您这样做,保持活动会话的时间也可能不会足够长,无法像FTP那样进行有意义的人机交互。 Most keep-alive sessions are automatically terminated after 5-15 seconds, depending on your web server's configuration. 大多数保持活动状态的会话会在5-15秒后自动终止,具体取决于您的Web服务器的配置。 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. 您也许可以对此进行调整,但是保持连接的生存期更长是一个非常糟糕的主意,尤其是在将Apache与mod_php结合使用时,因为持久的连接会垄断服务器资源,但会损害其他用户的利益。 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. 只需使用会话和/或Cookie。 They always work. 他们总是工作。

If you are not limited to apache, and can simply run php scripts I think you can do it. 如果您不仅限于Apache,并且可以简单地运行php脚本,我想您可以做到。

http://php.net/manual/en/book.sockets.php http://php.net/manual/zh/book.sockets.php

PHP allows you to open a specific port and have low level read writes on it. PHP允许您打开特定的端口并对其进行低级读写。 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. 对于这种特殊情况,您可以让Apache在非HTTP端口上运行,并从自定义PHP脚本监听HTTP端口。 You shall then authenticate the request on connection; 然后,您将在连接时验证请求; append a custom header(call it myauth) for user and then forward the request to apache. 为用户添加一个自定义标头(称为myauth),然后将请求转发给apache。 Make sure you filter out the myauth header while reading the original http request. 确保在读取原始http请求时过滤出myauth标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM