0

I am new to website development.

I need to capture the client's IP address. I checked similar question https://stackoverflow.com/questions/33268683/how-to-get-client-ip-address-in-laravel-5 but couldn't understand much.

I used request()->ip() but it gave me google cloud's ip everytime as website is on it and 127.0.0.1 locally. I want to get client's ip() who visits my website. How to get it?

I used $myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com")); which locally give me my public IP address. Will this work when I upload my code on google cloud ?

and also can someone tell me $myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com")); how this command works?

  • Does this answer your question? [What is the most accurate way to retrieve a user's correct IP address in PHP?](https://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php) – g9m29 Aug 19 '20 at 09:09

2 Answers2

0

This line:

$myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));

is not checking for the visitor IP. You are checking the IP from the command runner (aka you / your server).

Now, $request->ip() just reads the REMOTE_ADDR header attribute.

I haven't used Google Cloud and I'm not sure about its behaviour, but if you're getting the Google Cloud's IP instead of the user's IP it must be because it is working as a proxy and it's not forwarding the real user IP.

I guess there should be a way to tell Google Cloud to forward the visitor IP on the request.

Professor
  • 858
  • 1
  • 9
  • 17
0

If you're using Google Cloud's load balancer, I believe you can access the forwarded IP address from $_SERVER['X-Forwarded-For'].

https://cloud.google.com/load-balancing/docs/https#x-forwarded-for_header

mllnd
  • 555
  • 3
  • 5
  • 16