Реальный ip адрес php

PHP most accurate / safe way to get real user IP address in 2017

What is the most accurate way to get user’s IP address in 2017 via PHP? I’ve read a lot of SO questions and answers about it, but most of answers are old and commented by users that these ways are unsafe. For example, take a look at this question (2011): How to get the client IP address in PHP? Tim Kennedy’s answer contains a recommendation to use something like:

if (!empty($_SERVER['HTTP_CLIENT_IP'])) < $ip = $_SERVER['HTTP_CLIENT_IP']; >elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) < $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; >else

But as I’ve read a lot, I have seen that to use X_FORWARDED_FOR is unsafe, as the comment below highlights:

Do NOT use the above code unless you know EXACTLY what it does! I’ve seen MASSIVE security holes due to this. The client can set the X-Forwarded-For or the Client-IP header to any arbitrary value it wants. Unless you have a trusted reverse proxy, you shouldn’t use any of those values.

As I didn’t know EXACTLY what it does, I don’t want to take the risk. He said it is unsafe, but did not provide a safe method to get user’s IP address. I’ve tried the simple $_SERVER[‘REMOTE_ADDR’]; , but this returns the wrong IP. I’ve tested this and my real IP follows this pattern: 78.57.xxx.xxx , but I get an IP address like: 81.7.xxx.xxx So do you have any ideas?

Before using IPs for anything you should really know what they are and how IP networks work. IPs are just an implementation detail of a data transport mechanism (TCP/IP). It doesn’t necessarily give you useful information beyond some detail on how the data of the current request was transported to your server. Only once you understand that should you consider using IPs for any other purpose.

10 Answers 10

Short answer:

As of 2021 (and still) $_SERVER[‘REMOTE_ADDR’]; is the only reliable way to get users ip address, but it can show erroneous results if behind a proxy server.
All other solutions imply security risks or can be easily faked.

Yes, and $_SERVER[‘REMOTE_ADDR’] returning the proxy IP is not wrong or incorrect; just not what you really want from it.

Um, what 4 days delay? I’ve posted my answer hours before yours . And I didn’t comment because I didn’t «like» a word, but because your usage of that word propagates a falsehood.

From a security POV, nothing but $_SERVER[‘REMOTE_ADDR’] is reliable — that’s just the simple truth, unfortunately.

All the variables prefixed with HTTP_ are in fact HTTP headers sent by the client, and there there’s no other way to transfer that information while requests pass through different servers.
But that of course automatically means that clients can spoof those headers.

Читайте также:  Python для новичков хабр

You can never, ever trust the client.

Unless it is you . If you control the proxy or load-balancer, it is possible to configure it so that it drops such headers from the original request.
Then, and only then, you could trust an e.g. X-Client-IP header, but really, there’s no need to at that point . your webserver can also be configured to replace REMOTE_ADDR with that value and the entire process becomes transparent to you.

This will always be the case, no matter which year we are in . for anything related to security — only trust REMOTE_ADDR .
Best case scenario is to read the HTTP_ data for statistical purposes only, and even then — make sure that the input is at least a valid IP address.

Thank you for an answer, but I’m thinking If It is safe at all to save ‘HTTP_X_FORWARDED.. to database, everyone tells that It’s unsafe and can be spoofed. So If user will change HTTP_X. to value like ‘ or anything like this, It could be SQL Injected?

Yes, it can . That’s why you need to validate that it is a valid IP address. You can do that easily with filter_var() and FILTER_VALIDATE_IP .

@Infinity SQL injection is an entirely separate topic that you must guard against by properly forming SQL queries, not by being afraid of what characters any one specific value may or may not contain.

You have to collaborate with your sysops team (or if you’re wearing that hat too, you will need to do some research). The header check is used when your network infrastructure is configured in certain ways where the remote requester is one of your network appliances instead of the end user.

This sort of thing happens when your web server(s) sit behind a load balancer or firewall or other appliance that needs to interrogate the payload to properly handle it. An example is when a load balancer terminated ssl and forwards the request on to the web server without ssl. When this occurs the remote address becomes the load balancer. It also happens with firewall appliances that do the same thing.

Most instances the device will offer configuration to set a header value in the request with the original remote ip address. The header is usually what you’d expect but it can in some cases be different or even configurable.

What’s more, depending on your web server configuration (apache, nginx or other) may not support or be currently configured to support certain custom headers such as the common ip header.

The point is us you will need to investigate your network configuration to ensure that the original requester’s ip makes it all the way through to your application code and in what form.

If you’d like to use a pre-built library, you can use Whip.

Using pre-made libraries are usually better because most of them will have been checked thoroughly by an active community. Some of them, especially the ones that have been around for a long time, have more features built-in.

But if you want to code it yourself to learn the concept, then it’s ok I guess. I recommend packaging it as a stand alone library and releasing it as open-source 🙂

EDIT: I do not recommend using the remote IP in security mechanisms as they are not always reliable.

First, it is impossible to reliably determine someone’s source IP address if they are intent on being hidden. Even something which today seems foolproof, will soon have a loophole (if it doesn’t already). As such, any answer below should be considered UNTRUSTED, which means that if you put all of your eggs in this basket, be prepared for someone to take advantage of it or circumvent it somehow.

I won’t get in to all the ways someone can circumvent IP tracking, because it is constantly evolving. What I will say is that it can be a useful tool for logging as long as you know that IP addresses can easily change or otherwise be masked.

Now, one big point to make is that there is a difference between a public IP address and a private IP address. In IPV4, routers are generally assigned one public IP address, which is all that a server-side language can actually grab, because it doesn’t see your client-side IP address. To a server, your computer doesn’t exist as a web-space. Instead, your router is all that matters. In turn, your router is the only thing that cares about your computer, and it assigns a private IP address (to which your 172...* address belongs) to make this work. This is good for you, because you can’t directly access a computer behind a router.

If you want to access a private IP address, you need to use JavaScript (client-side language). You can then store the data asynchronously via AJAX. As far as I know, this is only currently possible using WebRTC-enabled Chrome and Firefox. See here for a demo.

I tested this and it returns private IP addresses. Typically I think this is used by advertisers to help track individual users in a network, in conjunction with the public IP address. I am certain that it will quickly become useless as people come up with workarounds or as public outcry forces them to offer the ability to disable the WebRTC API. However, for the time being it works for anyone who has JavaScript enabled on Chrome and Firefox.

Источник

How to get real IP of user using PHP? [duplicate]

I’ve been trying to get real IP address of the user, and not a proxy address. For that I’ve done this:

$ip1 = $_SERVER['REMOTE_ADDR']; $ip2 = $_SERVER['HTTP_X_FORWARDED_FOR']; $ip3 = $_SERVER['HTTP_FORWARDED']; mail("me@domain.com", "Report", "IP1 is $ip1, IP2 is $ip2, IP3 is $ip3 ."); 
IP is [proxy_addr_here], IP2 is , IP3 is . 

Is there any way to get real IP just like whatismyip.com tells (it tells real IP address, proxy address and useragent)? Update : Whatismyip tells me this «Your IP Address Is: [my real IP] Proxy: [my proxy address] City: Alipur State/Region: Delhi Country: IN — ISP: Bharti Airtel Ltd.» How come it gathers all the details so accurately but not my PHP script?

That’s because this isn’t really possible in the general sense. Many proxies do not include the headers reporting who they are proxying for. On purpose.

2 Answers 2

$ip= isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; 

But even that is not 100%. Getting IP of the real user now a days is not a guarantee with so many NAT firewalls in between, let alone proxies.

To answer your edit as to how they show you more information, you could use many features of the language to get more request details. For example try

And extract the required information from there.

Even your favorite whatismyip.com reports

Your IP Location can be found using our IP Lookup tool. No IP Lookup tool is 100% accurate due to many different factors. Some of those factors include where the owner of the IP has it registered, where the agency that controls the IP is located, proxies, cellular IPs, etc. If you are in the US and the controlling agency of the IP is located in Canada, chances are the IP address lookup results will show as Canada. Showing a Canadian IP while in the US is very common among Blackberry users on the Verizon network.

You might still ask ok then why cant I get at least what they show? Im sure they have put up a lot of research and resources in setting up that tool, its not a one line PHP code, in fact there is no telling whether that site is written in PHP at all. Some more research and you will be on your way to try to match their lookup.

Источник

Оцените статью