Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T16:00:23.053Z
Answers.Text: Thanks Brad - I'd searched the docs using 'ip ' and should've used 'ipa'!
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T16:05:33.960Z
Answers.Text: Actually, is there any reason {{ craft.request.getIpAddress() }} would be empty?
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T17:41:03.960Z
Answers.Text: If your server isn't populating `$_SERVER['REMOTE_ADDR']`?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T13:04:21.867Z
Answers.Text: 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.
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T13:30:19.713Z
Answers.Text: And have tried this on two sites on different server setups, too.
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T17:55:16.503Z
Answers.Text: Gah, sorry.  Looks like we don't expose that to the template level. I'll update my answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T15:55:50.743Z
Answers.LastActivityDate: 2015-04-16T12:04:45.587Z
Answers.LastEditDate: 2015-04-16T12:04:45.587Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-22T10:46:09.567Z
Answers.Text: 2021 reference: `craft.request.getIpAddress()` has been deprecated. Use `craft.app.request.userIP` instead.
Answers.UserDisplayName: Dom Stubbs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-07T19:50:11.207Z
Answers.LastActivityDate: 2019-06-07T19:50:11.207Z
Answers.UserDisplayName: Avant la lettre
Body: <p>I just want to run a couple of conditionals depending on a visitor's IP address. How would I do that?</p>
  
Tags: <conditional><ip-address>
Title: How to get IP address without a plugin?
ThreadText: # 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](https://buildwithcraft.com/classreference/services/HttpRequestService#getIpAddress-detail) 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 username_1 - 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.  
  
  ---
  
  