<p>Between the two, I'd say that <code>craft()-&gt;request-&gt;userHostAddress</code> is going to be a more reliable method.   It tries to take into account things like clients going though proxy servers, shared ISP IP addresses, etc. whereas you don't know any of that just relying on <code>$_SERVER['REMOTE_ADDR']</code>.</p>

<p>But, as you pointed out, none of this is guaranteed to be reliable.</p>

CC BY-SA 3.0
2015-07-06T13:25:56.677Z
2015-07-06T13:25:56.677Z
Brad Bell
<p>The typical way to get your user's IP address via PHP is with <code>$_SERVER['REMOTE_ADDR']</code>. This is pretty solid, but can lead to some IP detections that are surprisingly far away from the user's actual location.</p>

<p>Craft also has it's own IP detection with <code>craft()-&gt;request-&gt;userHostAddress</code>. This jumps through a few more hoops before finally falling back to <code>$_SERVER['REMOTE_ADDR']</code>.</p>

<p><strong>Which is better in terms of geolocation accuracy?</strong></p>

<p>The Craft docs say <a href="http://buildwithcraft.com/classreference/services/HttpRequestService#getIpAddress-detail" rel="nofollow">this</a>:</p>

<blockquote>
  <p>Considering any of these server vars besides REMOTE_ADDR can be spoofed, this method should not be used when you need a trusted source for the IP address. Use <code>$_SERVER['REMOTE_ADDR']</code> instead.</p>
</blockquote>

<p>I don't think my source needs to be that "trusted". I'm using the IP address for <em>best-guess geolocation</em>, not for any sort of security purposes.</p>

<p><strong>I'm just looking for whichever one will pinpoint closest to the human user.</strong></p>

<hr>

<p><strong>Disclaimer:</strong> I'm well aware that IP detection is a lousy way to pinpoint users, and that HTML5 / JS offers much more accurate geolocation results. But when using that method, geolocation is optional <em>to the user</em>, and they may deny permission to share their location. It's good to have the IP detection as a fallback.</p>

<plugin-development><ip-address>
$_SERVER['REMOTE_ADDR'] vs craft()->request->userHostAddress
# $_SERVER['REMOTE_ADDR'] vs craft()->request->userHostAddress

(Asked by: username_0 on 2015-07-06)

The typical way to get your user's IP address via PHP is with `$_SERVER['REMOTE_ADDR']`. This is pretty solid, but can lead to some IP detections that are surprisingly far away from the user's actual location.

Craft also has it's own IP detection with `craft()->request->userHostAddress`. This jumps through a few more hoops before finally falling back to `$_SERVER['REMOTE_ADDR']`.

**Which is better in terms of geolocation accuracy?**

The Craft docs say [this](http://buildwithcraft.com/classreference/services/HttpRequestService#getIpAddress-detail):

> Considering any of these server vars besides REMOTE\_ADDR can be spoofed, this method should not be used when you need a trusted source for the IP address. Use `$_SERVER['REMOTE_ADDR']` instead.

I don't think my source needs to be that "trusted". I'm using the IP address for *best-guess geolocation*, not for any sort of security purposes.

**I'm just looking for whichever one will pinpoint closest to the human user.**

---

**Disclaimer:** I'm well aware that IP detection is a lousy way to pinpoint users, and that HTML5 / JS offers much more accurate geolocation results. But when using that method, geolocation is optional *to the user*, and they may deny permission to share their location. It's good to have the IP detection as a fallback.

**Tags:** plugin-development, ip-address


---

## 1 answers

### Answer by username_1 on 2015-07-06. Score: 1 | Accepted answer

Between the two, I'd say that `craft()->request->userHostAddress` is going to be a more reliable method. It tries to take into account things like clients going though proxy servers, shared ISP IP addresses, etc. whereas you don't know any of that just relying on `$_SERVER['REMOTE_ADDR']`.

But, as you pointed out, none of this is guaranteed to be reliable.


---

