<p>From PHP:</p>

<pre><code>if (craft()-&gt;request-&gt;getIpAddress() == '255.255.255.255')
{
    ...
}
</code></pre>

<p>Note <a href="https://buildwithcraft.com/classreference/services/HttpRequestService#getIpAddress-detail" rel="noreferrer">from the docs</a> that this is a "best guess" and can easily be spoofed, so you shouldn't use it to hide any sensitive information.</p>

<p>We currently don't expose that method at the template level, but we probably should. I'll add that on the Craft 2.3 branch.</p>

<p>If you want to hack it in before then, you can open up <code>craft/app/variables/HttpRequestVariable.php</code> and add:</p>

<pre><code>public function getIpAddress()
{
    return craft()-&gt;request-&gt;getIpAddress();
}
</code></pre>

<p>Then from a template you'd use:</p>

<pre><code>{% if craft.request.getIpAddress() == '255.255.255.255' %}
    ...
{% endif %}
</code></pre>

<p>Just note that there is a chance that a 2.2 update could overwrite that file at any time.</p>

<p>UPDATE: No hack is required since Craft 2.3.2615.</p>

CC BY-SA 3.0
2014-11-05T16:00:23.053Z
Thanks Brad - I'd searched the docs using 'ip ' and should've used 'ipa'!
Clive Portman
CC BY-SA 3.0
2014-11-05T16:05:33.960Z
Actually, is there any reason {{ craft.request.getIpAddress() }} would be empty?
Clive Portman
CC BY-SA 3.0
2014-11-05T17:41:03.960Z
If your server isn't populating `$_SERVER['REMOTE_ADDR']`?
Brad Bell
CC BY-SA 3.0
2014-11-06T13:04:21.867Z
This is strange. In a simple PHP file I can echo $_SERVER['REMOTE_ADDR'] but if I put {{ craft.request.getIpAddress() }} at the top of my layout template, nothing is output.
Clive Portman
CC BY-SA 3.0
2014-11-06T13:30:19.713Z
And have tried this on two sites on different server setups, too.
Clive Portman
CC BY-SA 3.0
2014-11-06T17:55:16.503Z
Gah, sorry.  Looks like we don't expose that to the template level. I'll update my answer.
Brad Bell
CC BY-SA 3.0
2014-11-05T15:55:50.743Z
2015-04-16T12:04:45.587Z
2015-04-16T12:04:45.587Z
Brad Bell
<p>To make use of your visitors IP address, use <code>{{ craft.request.getIpAddress() }}</code> in Craft 2.5. In Craft 3.x, there is <code>{{ craft.app.request.userIP }}</code>.</p>

CC BY-SA 4.0
2021-10-22T10:46:09.567Z
2021 reference: `craft.request.getIpAddress()` has been deprecated. Use `craft.app.request.userIP` instead.
Dom Stubbs
CC BY-SA 4.0
2019-06-07T19:50:11.207Z
2019-06-07T19:50:11.207Z
Avant la lettre
<p>I just want to run a couple of conditionals depending on a visitor's IP address. How would I do that?</p>

<conditional><ip-address>
How to get IP address without a plugin?
# How to get IP address without a plugin?

(Asked by: username_0 on 2014-11-05)

I just want to run a couple of conditionals depending on a visitor's IP address. How would I do that?

**Tags:** conditional, ip-address

---

## 2 answers

### Answer by username_2 on 2019-06-07. Score: 9 | Accepted answer

To make use of your visitors IP address, use `{{ craft.request.getIpAddress() }}` in Craft 2.5. In Craft 3.x, there is `{{ craft.app.request.userIP }}`.

> **Comments:**
> **username_3**: 2021 reference: `craft.request.getIpAddress()` has been deprecated. Use `craft.app.request.userIP` instead.  

---

### Answer by username_1 on 2014-11-05. Score: 9

From PHP:
if (craft()->request->getIpAddress() == '255.255.255.255')
{
    ...
}

Note from the docs that this is a "best guess" and can easily be spoofed, so you shouldn't use it to hide any sensitive information.
We currently don't expose that method at the template level, but we probably should. I'll add that on the Craft 2.3 branch.
If you want to hack it in before then, you can open up craft/app/variables/HttpRequestVariable.php and add:
public function getIpAddress()
{
    return craft()->request->getIpAddress();
}

Then from a template you'd use:
{% if craft.request.getIpAddress() == '255.255.255.255' %}
    ...
{% endif %}

Just note that there is a chance that a 2.2 update could overwrite that file at any time.
UPDATE: No hack is required since Craft 2.3.2615.

> **Comments:**
> **username_0**: Thanks Brad - I'd searched the docs using 'ip ' and should've used 'ipa'!  
> **username_0**: Actually, is there any reason {{ craft.request.getIpAddress() }} would be empty?  
> **username_1**: If your server isn't populating `$_SERVER['REMOTE_ADDR']`?  
> **username_0**: This is strange. In a simple PHP file I can echo $_SERVER['REMOTE_ADDR'] but if I put {{ craft.request.getIpAddress() }} at the top of my layout template, nothing is output.  
> **username_0**: And have tried this on two sites on different server setups, too.  
> **username_1**: Gah, sorry.  Looks like we don't expose that to the template level. I'll update my answer.  

---

