一个php获取网站域名与IP地址的函数,代码如下:
- <?
- function PMA_getenv($var_name) {
- if (isset($_SERVER[$var_name])) {
- return $_SERVER[$var_name];
- } elseif (isset($_ENV[$var_name])) {
- return $_ENV[$var_name];
- } elseif (getenv($var_name)) {
- return getenv($var_name);
- } elseif (function_exists('apache_getenv')
- && apache_getenv($var_name, true)) {
- return apache_getenv($var_name, true);
- }
- return '';
- }
- if (emptyempty($HTTP_HOST)) {
- if (PMA_getenv('HTTP_HOST')) {
- $HTTP_HOST = PMA_getenv('HTTP_HOST');
- } else {
- $HTTP_HOST = '';
- }
- }
- echo htmlspecialchars($HTTP_HOST);
- ?>
我把获取IP地址的代码放进去,代码如下:
- <?php
- function GetIP() {
- if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
- $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
- else if (isset($_SERVER["HTTP_CLIENT_IP"]))
- $ip = $_SERVER["HTTP_CLIENT_IP"];
- else if (isset($_SERVER["REMOTE_ADDR"]))
- $ip = $_SERVER["REMOTE_ADDR"];
- else if (getenv("HTTP_X_FORWARDED_FOR"))
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- else if (getenv("HTTP_CLIENT_IP"))
- $ip = getenv("HTTP_CLIENT_IP");
- else if (getenv("REMOTE_ADDR"))
- $ip = getenv("REMOTE_ADDR");
- else
- $ip = "Unknown";
- return $ip;
- }
- echo GetIP();
- ?>
新闻热点
疑难解答