note 59511 added to function.getenv

From: Date: Thu, 08 Dec 2005 23:03:58 +0000
Subject: note 59511 added to function.getenv
Groups: php.notes 
Request: Send a blank email to php-notes+get-99777@lists.php.net to get a copy of this message
Here is a nice getip function set for your sites. Since most of the functions below have *most* of the features, but not all, I figured I'd consolidate and add protections for those proxies that don't always follow RFCs to the letter. getip() will return the most accurate client IP possible: <?php function validip($ip) { if (!empty($ip) && ip2long($ip)!=-1) { $reserved_ips = array ( array('0.0.0.0','2.255.255.255'), array('10.0.0.0','10.255.255.255'), array('127.0.0.0','127.255.255.255'), array('169.254.0.0','169.254.255.255'), array('172.16.0.0','172.31.255.255'), array('192.0.2.0','192.0.2.255'), array('192.168.0.0','192.168.255.255'), array('255.255.255.0','255.255.255.255') ); foreach ($reserved_ips as $r) { $min = ip2long($r[0]); $max = ip2long($r[1]); if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false; } return true; } else { return false; } } function getip() { if (validip($_SERVER["HTTP_CLIENT_IP"])) { return $_SERVER["HTTP_CLIENT_IP"]; } foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) { if (validip(trim($ip))) { return $ip; } } if (validip($_SERVER["HTTP_X_FORWARDED"])) { return $_SERVER["HTTP_X_FORWARDED"]; } elseif (validip($_SERVER["HTTP_FORWARDED_FOR"])) { return $_SERVER["HTTP_FORWARDED_FOR"]; } elseif (validip($_SERVER["HTTP_FORWARDED"])) { return $_SERVER["HTTP_FORWARDED"]; } elseif (validip($_SERVER["HTTP_X_FORWARDED"])) { return $_SERVER["HTTP_X_FORWARDED"]; } else { return $_SERVER["REMOTE_ADDR"]; } } ?> Enjoy, and be sure to share your best scripts in other help pages! ---- Manual Page -- http://www.php.net/manual/en/function.getenv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59511 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59511&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59511&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59511&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59511&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59511&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59511&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59511&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59511&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99777) next »