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

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

本文介绍了如何使用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变量:

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 ['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地址和端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:59