PHP implements five methods for obtaining ip address and an example of inserting user login log

  • 2021-11-29 23:13:44
  • OfStack

This paper describes five methods of obtaining ip address by PHP and inserting user login log. Share it for your reference, as follows:

php 5 methods to get ip address, insert user login log instance, the second method is recommended


<?php  // Method 1 : 
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
// Method 2 : 
$ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$ip = ($ip) ? $ip : $_SERVER["REMOTE_ADDR"];
echo $ip;
// Method 3 : 
function getRealIp()
{
  $ip=false;
  if(!empty($_SERVER["HTTP_CLIENT_IP"])){
    $ip = $_SERVER["HTTP_CLIENT_IP"];
  }
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
    if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
    for ($i = 0; $i < count($ips); $i++) {
      if (!eregi ("^(10 The 172.16 The 192.168).", $ips[$i])) {
        $ip = $ips[$i];
        break;
      }
    }
  }
  return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
echo getRealIp();
// Method 4 : 
if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
{
  $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
}
elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
{
  $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
}
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
{
  $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
}
elseif (getenv("HTTP_X_FORWARDED_FOR"))
{
  $ip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP"))
{
  $ip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("REMOTE_ADDR"))
{
  $ip = getenv("REMOTE_ADDR");
}
else
{
  $ip = "Unknown";
}
echo $ip ;
// Method 5 : 
if(getenv('HTTP_CLIENT_IP')) {
  $onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
  $onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR')) {
  $onlineip = getenv('REMOTE_ADDR');
} else {
  $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
}
echo $onlineip;

Example: Get the user IP when the user logs in and insert it into the database login log table


// Add user login log 
$ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$ip = ($ip) ? $ip : $_SERVER["REMOTE_ADDR"];
$this->usermodel->addUserlog($userid,$nickname,$ip);
function addUserlog($userid,$nickname,$ip) {
    try {
      $now = date("Y-m-d H-i-s",time());
      $data=array(
        'userid'=>$userid,
        'nickname'=>$nickname,
        'ip'=>$ip,
        'logintime'=>$now
      );
      $insert_query = $this->db->insert_string('user_log', $data);
      $this->db->query($insert_query);
      return 0;
    } catch ( Exception $e ) {
      return -1;
    }
}

Log table structure:


CREATE TABLE `user_log` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `userid` bigint(20) NOT NULL,
 `nickname` varchar(50) NOT NULL,
 `ip` varchar(50) NOT NULL,
 `logintime` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

For more readers interested in PHP related contents, please check the special topics of this site: Summary of PHP Network Programming Skills, Summary of php curl Usage, Encyclopedia of PHP Array (Array) Operation Skills, Summary of php String (string) Usage, Tutorial of PHP Data Structure and Algorithm, Summary of php Programming Algorithm, Summary of PHP Mathematical Operation Skills and Summary of php Common Database Operation Skills

I hope this article is helpful to everyone's PHP programming.


Related articles: