Search code examples
laraveloctane

Undefined array key "REMOTE_ADDR" in laravel octane


I am using laravel 8. Before upgarding to octane I was getting ip address in $_SERVER['REMOTE_ADDR']. After changing to octane there is no such index in $_SERVER.

I am using this variable in validator of register controller ,while new user is registering to app.

 protected function validator(array $data)
      {
 dd($_SERVER['REMOTE_ADDR'])
}

when I dump $_SERVER I am getting this array (I am hiding app details.)

  "LARAVEL_OCTANE" =>"1"
  "APP_BASE_PATH" => "xxx"
  "JOURNAL_STREAM" => "xxx"
  "PATH" =>"xxx"
  "INVOCATION_ID" => "xxx"
  "LANG" => "xxx"
  "SUPERVISOR_ENABLED" =>"xxx"
  "SUPERVISOR_SERVER_URL" => "xxx"
  "SUPERVISOR_PROCESS_NAME" =>"xxx"
  "APP_ENV" => "xxx"
  "PWD" => "xxx"
  "PHP_SELF" => "xxx"
  "SCRIPT_NAME" => "xxx"
  "SCRIPT_FILENAME" =>"xxx"
  "PATH_TRANSLATED" => "xxx"
  "DOCUMENT_ROOT" => ""
  "REQUEST_TIME_FLOAT" => "xxx"
  "REQUEST_TIME" => "xxx"
  "argv" => array:2 [▶]
  "argc" =>"xxx"
  "APP_NAME" => "xxx"
  "APP_KEY" => "xxx"
  "APP_DEBUG" => "xxx"
  "APP_LOG_LEVEL" => "xxx"
  "APP_URL" => "xxx"
  "OCTANE_SERVER" =>"xxx"
  "LOG_CHANNEL" => "xxx"
  "DB_CONNECTION" => "xxx"
  "DB_HOST" => "xxx"
  "DB_PORT" => "xxx"
  "DB_DATABASE" => "xxx"
  "DB_USERNAME" =>"xxx"
  "DB_PASSWORD" => "xxx"
  "BROADCAST_DRIVER" => "xxx"
  "CACHE_DRIVER" => "xxx"
  "SESSION_DRIVER" => "xxx"
  "SESSION_LIFETIME" => "xxx"
  "QUEUE_CONNECTION" => "xxx"
  ...............
 


Solution

  • I have found the solution for this problem. As @Rwd commented replaced $_SERVER['REMOTE_ADDR'] by request()->ip()

    and it works correctly . (In octane the global variables of PHP will not be initialized when the request is initialized, such as$_ SERVER, $_ POST, $_ Get, etc.. Got details from following article. https://developpaper.com/php-fpm-vs-swoole/)