I want to save the ip address of the client in my database. Heres my code to do this:
// app/Error.php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class Error extends Model {
protected $fillable = array( 'code', 'user', 'error', 'client', 'call');
public static function log( $code="unset", $message, $call="unset" ) {
$kyu = "foobar";
$uri = "unset";
$ip = "unset";
$agent = "unset";
if ( isset( $_SERVER ) ) {
if ( array_key_exists( 'REQUEST_URI', $_SERVER ) ) {
$uri = $_SERVER['REQUEST_URI'];
}
if ( array_key_exists( 'REMOTE_ADDR', $_SERVER ) ) {
$ip = $_SERVER['REMOTE_ADDR'];
}
if ( array_key_exists( 'HTTP_USER_AGENT', $_SERVER ) ) {
$agent = $_SERVER['HTTP_USER_AGENT'];
}
}
$callOnUri = $call . ' on: ' . $uri;
// create database entry
$user = Error::create([
'code' => $code,
'user' => $kyu,
'ip' => $ip, // this field is nullable in the migration
'client' => $agent,
'error' => $message,
'call' => $callOnUri // get the call out of the request
]);
}
}
When I call this code, the $client and $uri is saved correctly, but the $ip is always: NULL. I checked on the browser with the following code, on the same request and on the browser I get the IP shown, but in the database is still NULL
public function postRegister()
{
$validator = $this->registrar->validator(\Input::all());
$returnVal = $this->registrar->create( \Input::all( ) );
if ( is_bool($returnVal) ) {
// foobar
} else {
Error::log( '1234', "Failure in the registration process.", __FUNCTION__ );
return response()->json(["Error" => $returnVal], "server" => $_SERVER); // dump of the $_SERVER
}
}
How do I get the ip of the client ( REMOTE_ADDR ) correctly saved in the database?