Skip to content

Instantly share code, notes, and snippets.

@devuri
Created October 21, 2017 08:37
Show Gist options
  • Select an option

  • Save devuri/a98c6c9cd189efd4a5d64f885cfac9c5 to your computer and use it in GitHub Desktop.

Select an option

Save devuri/a98c6c9cd189efd4a5d64f885cfac9c5 to your computer and use it in GitHub Desktop.
get IP
<?php
function get_ip() {
$ip = '127.0.0.1';
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// Fix potential CSV returned from $_SERVER variables
$ip_array = array_map( 'trim', explode( ',', $ip ) );
return $ip_array[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment