如何使用PHP获取客户端的IP地址和端口? [英] How can I get the IP address and port of a client with PHP?

查看:2058
本文介绍了如何使用PHP获取客户端的IP地址和端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP获取客户端的IP和端口?

How do I get the IP and port of a client with PHP?

我尝试了下面的脚本,但它只给了我IP地址。

I tried the script below but it only gives me the IP address.

<?php print $_SERVER['REMOTE_ADDR']; ?>


推荐答案

端口在http服务器(Apache或其他和主要是80或443)

Port is defined in http server (Apache or other and mostly it is 80 or 443)

您可以在以下网址查看php $ _SERVER变量: http://www.php.net/manual/en/reserved.variables.server.php

The php $_SERVER variables you can check at : http://www.php.net/manual/en/reserved.variables.server.php

我确信:
REMOTE_ADDR'
用户查看当前页面的IP地址。

I am sure that : REMOTE_ADDR' The IP address from which the user is viewing the current page.

但是如果你的服务器在NAT后面:

But if your server is behind NAT:


如果你是从代理服务器后面服务,你几乎可以节省
这些$ _SERVER变量在你的机器后面的
上做了多少代理。

If you are serving from behind a proxy server, you will almost certainly save time by looking at what these $_SERVER variables do on your machine behind the proxy.

$ _ SERVER ['HTTP_X_FORWARDED_FOR']代替$ _SERVER ['REMOTE_ADDR']

$_SERVER['HTTP_X_FORWARDED_FOR'] in place of $_SERVER['REMOTE_ADDR']

$ _ SERVER [ 'HTTP_X_FORWARDED_HOST']和
$ _SERVER ['HTTP_X_FORWARDED_SERVER']代替
$ _SERVER ['SERVER_NAME']

$_SERVER['HTTP_X_FORWARDED_HOST'] and $_SERVER['HTTP_X_FORWARDED_SERVER'] in place of $_SERVER['SERVER_NAME']

: - )

这篇关于如何使用PHP获取客户端的IP地址和端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆