0

Background

I am creating a REST API to work with my mobile app. It is important that I keep a record of any request made to the server by IP address for legality purposes.

I understand people can spoof the IP if they want, but that is not a concern. There is an API key for security, this is simply a CYA precaution.

Using the middleware Slim 3 suggest here the value is always NULL I am testing this from my localhost (If that makes a difference, but I do not know why it would).

Problem

When I hit the get request endpoint and I try to capture the IP it is always NULL.

Example

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

    $app->get('/api/v1/customer', function (Request $request, Response $response) {
        $requestIP = $request->getAttribute('ip_address');
         var_dump($requestIP);
         echo $requestIP;
         echo 'CUSOTMERS!';

        return $response;
    });

$app->run();

Output

NULL CUSOTMERS!

Question

What is the proper way to capture the IP address from the request using this rka-ip-address-middleware middleware in SlimFrameWork 3

wuno
  • 9,547
  • 19
  • 96
  • 180
  • Are you actually adding the middleware somewhere? It is not shown in the example code above. – Mika Tuupola Jun 18 '17 at 11:30
  • @MikaTuupola yes thanks mike. I was under the impression that line was for if you needs to pass parameters of accepted IP. I did not realize you can call it with no params. It's all working now. – wuno Jun 18 '17 at 19:41

1 Answers1

4

Did you try something like this?

$requestIP = $request->getServerParam('REMOTE_ADDR');
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33
Valery K.
  • 147
  • 1
  • 5