Skip to content

Instantly share code, notes, and snippets.

View renorram's full-sized avatar
🚀
being awesome

Renorram Brandão renorram

🚀
being awesome
  • Enschede, Netherlands
View GitHub Profile
@renorram
renorram / get-ip-address-optimized.php
Created August 3, 2016 17:29 — forked from cballou/get-ip-address-optimized.php
PHP - Advanced Method to Retrieve Client IP Address
<?php
function get_ip_address() {
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($ip_keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
// trim for safety measures
$ip = trim($ip);
// attempt to validate IP
if (validate_ip($ip)) {