简体   繁体   中英

Is getting an IP address with JavaScript as accurate as getting it through PHP?

I'm just wondering, is using JavaScript to get an IP like described in How to get client's IP address using javascript only? just as accurate as using

$_SERVER['REMOTE_ADDR'];

in PHP or another server-side language? What is the different in how the information is extracted in each?

For some background information, I know that Google Analytics is JS code and supposedly I can filter out IPs in my GA dashboard and I'm wondering how that is done and if it's reliable.

You can't truly get an IP with JavaScript. All the examples on the page you listed get it from a server or API. PHP is your best option aside from using an existing API. Either way, it's not going to be extremely reliable and can easily be faked.

No. You cannot trust JavaScript for critical information.

The best way to get an IP address with PHP is:

$_SERVER['REMOTE_ADDR'];

Using 'REMOTE_ADDR' returns the actual physical IP address of the client.


Depending on your application requirements, it is sometimes helpful to get a client ip that is behinda proxy. You can use the following variable:

$_SERVER['HTTP_X_FORWARDED_FOR'];

It is important to note however that 'HTTP_X_FORWARDED_FOR' can be easily spoofed.


Google Analytical uses JavaScript to get client PC details like:

  1. Screen size and resolution
  2. Is mobile device?
  3. Java & Flash version
  4. Language (Browser)
  5. User Agent
  6. Operating System

It is important to remember that all this information can be spoofed. However, if a genuine visitor is visiting your website it is very unlikely that this information will return false information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM