2

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?

jerik
  • 5,714
  • 8
  • 41
  • 80
  • Checkout the second answer in this - http://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php – Styphon Aug 13 '15 at 16:23
  • 4
    Also, what field type have you used to save it? It needs to be varchar, not int, – Styphon Aug 13 '15 at 16:23
  • Since you're setting the `$ip` variable to `"unset"`, and when you retrieve the row from database you get `NULL`, it seems to me you have a data type issue. Like @Styphon said above, I'd suggest you to check the type of the column where you're storing the IP. – Gustavo Straube Aug 13 '15 at 16:27
  • Sounds like a database issue to me as per the previous comment, is the 'ip' field at least a varchar(15)? You could try appending $ip to $message to check that it is actually getting the IP address... – james-geldart Aug 13 '15 at 16:28
  • I always enjoy looking at skillful programmers who are able to turn object oriented framework in a pile of procedural garbage. You didn't make the `ip` fillable. The other "problem" is that you're accessing superglobals for no valid reason. Every `Request` object contains an IP address. All of this could have been written in 2 lines of code.. – N.B. Aug 14 '15 at 14:46
  • @N.B. `ip` did the trick. Thanks. Thanks as well for the objective answer. I am sure you are one of the persons who never makes mistakes :) – jerik Aug 14 '15 at 20:36
  • @jerik of course I do, all the time, I'm the first weak link I suspect. I'm just saying that you could have done this much easier on yourself, no offense. You just produced too much code and you missed a simple thing. Just pass a `Request` to your model and use `getClientIp()` method, you can easily get rid of all that superglobal checking. – N.B. Aug 14 '15 at 20:52

1 Answers1

1

You need to add the ip in $fillable.

protected $fillable = array( 'code', 'user', 'error', 'ip', 'client', 'call');
Hasan Tareque
  • 1,761
  • 11
  • 21