php获取网站域名与IP地址的函数

一个php获取网站域名与IP地址的函数,代码如下:

  1. <?
  2. function PMA_getenv($var_name) {
  3. if (isset($_SERVER[$var_name])) {
  4. return $_SERVER[$var_name];
  5. } elseif (isset($_ENV[$var_name])) {
  6. return $_ENV[$var_name];
  7. } elseif (getenv($var_name)) {
  8. return getenv($var_name);
  9. } elseif (function_exists('apache_getenv')
  10. && apache_getenv($var_name, true)) {
  11. return apache_getenv($var_name, true);
  12. }
  13. return '';
  14. }
  15. if (emptyempty($HTTP_HOST)) {
  16. if (PMA_getenv('HTTP_HOST')) {
  17. $HTTP_HOST = PMA_getenv('HTTP_HOST');
  18. } else {
  19. $HTTP_HOST = '';
  20. }
  21. }
  22. echo htmlspecialchars($HTTP_HOST);
  23. ?>

我把获取IP地址的代码放进去,代码如下:

  1. <?php
  2. function GetIP() {
  3. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
  4. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  5. else if (isset($_SERVER["HTTP_CLIENT_IP"]))
  6. $ip = $_SERVER["HTTP_CLIENT_IP"];
  7. else if (isset($_SERVER["REMOTE_ADDR"]))
  8. $ip = $_SERVER["REMOTE_ADDR"];
  9. else if (getenv("HTTP_X_FORWARDED_FOR"))
  10. $ip = getenv("HTTP_X_FORWARDED_FOR");
  11. else if (getenv("HTTP_CLIENT_IP"))
  12. $ip = getenv("HTTP_CLIENT_IP");
  13. else if (getenv("REMOTE_ADDR"))
  14. $ip = getenv("REMOTE_ADDR");
  15. else
  16. $ip = "Unknown";
  17. return $ip;
  18. }
  19. echo GetIP();
  20. ?>