LoginSignup
0
0

More than 1 year has passed since last update.

PHP ロードバランサーを利用時のIPアドレスを取得する方法

Posted at
<?php

namespace App\Libs\Server;

/**
 * サーバーの解析を担当
 */
class Analysis
{
    /**
     * アクセスユーザのIPアドレスを取得
     * ロードバランサー対応。
     */
    public static function getClientIpAddress() :string
    {
        $ip = '';
        if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
            $xForwardedFor = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if (!empty($xForwardedFor)) {
                $ip = trim($xForwardedFor[0]);
            }
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = (string)$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0