lolalola lolalola - 11 months ago 244
PHP Question

Get user ip address in zend framework

How can I get the client's IP address in Zend-framework 2? It'd be $_SERVER['REMOTE_ADDR'] in plain PHP, but maybe is smart Zend function?

Any ideas?


The request object(s) in ZF2 has method named getServer. This method returns an object implementing \Zend\Stdlib\ParametersInterface. With this particular object you can get anything from the $_SERVER variable.

Here are two examples of how to use the method and object:

    // Getting the entire params object
    $servParam = $request->getServer();
    $remoteAddr = $servParam->get('REMOTE_ADDR');

    // Getting specific variable
    $remoteAddr = $request->getServer('REMOTE_ADDR');

Hope this helps :)