php "SERVER['REMOTE_PORT']" 正确性

php "SERVER['REMOTE_PORT']" correctness

在我编写的旨在提供有关用户的更多数据的代码中,我很难收集用户的端口号。

我写在PHP.

<?php

$ip = $_SERVER['REMOTE_ADDR'];
$port = $_SERVER['REMOTE_PORT'];

?>

虽然用户的IP地址很容易通过命令收集:$_SERVER['REMOTE_ADDR'],

我得到一个与用户端口号不匹配的端口号,使用命令:$_SERVER['REMOTE_PORT'];

那么,$_SERVER['REMOTE_PORT']命令是不是应该提供用户的真实端口号?还是只是 return 一个随机数字?

有没有办法正确收集用户的端口号?

谢谢

$_SERVER['REMOTE_PORT'] 是在用户机器上发出请求的端口。这将用于将页面请求路由回他们的路由器。这可能看起来像一个随机数,但它是由用户的路由器生成的端口。

$_SERVER['SERVER_PORT'] 是您 web-server 上接收请求的端口。这将是 80443,具体取决于用户是否使用 https。

Here is another posted question on Super User that explains this phenomenon of browsers requesting a webpage from a "random" port -> https://superuser.com/questions/1055281/do-web-browsers-use-different-outgoing-ports-for-different-tabs.

此外,客户端和 web-server 之间的路由器可能会更改端口,然后将 port-connections 存储在 NAT(网络地址 table)中,以便将响应路由回去到浏览器。 -> https://superuser.com/questions/105838/how-does-router-know-where-to-forward-packet

来自:CompTIA Security+ Get Certified Get Ahead: SY0-501 Study Guide - 达里尔·吉布森 (Darril Gibson) 2017 年

Client-side ports start at ports 49,152 and increment up to 65,535.... ...When you use your web browser to request a page from a site, your system will record an unused client port number such as 49,152 in an internal table to handle the return traffic. When the web server returns the web page, it includes the client port as a destination port. When the client receives web page packets with a destination port of 49,152, it sends these packets to the web browser application. The browser processes the packets and displays the page.

此外,来自 PHP manual: https://www.php.net/manual/en/reserved.variables.server.php

'SERVER_PORT' The port on the server machine being used by the web server for communication. For default setups, this will be '80'; using SSL, for instance, will change this to whatever your defined secure HTTP port is.

'REMOTE_PORT' The port being used on the user's machine to communicate with the web server.