ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Link Based on User's IP Address

February 22, 2010 1:21pm

Subscribe [2]
  • #1 / Feb 22, 2010 1:21pm

    ParisJC

    150 posts

    We have certain services on campus that are accessed by students and faculty in different ways, depending on whether they are accessing via our campus network or from off-campus. Rather than utilize two links, I’d like some way for the user’s IP address to be recognized (i.e., is on-campus or not) and the appropriate link provided automatically (or via a redirect of some type).

    Such a thing possible? Anyone ever done this?

  • #2 / Feb 22, 2010 1:42pm

    lebisol

    2234 posts

    Can you use “logged in/out” instead of IP?
    If you have a DNS server on your campus you can always setup Forward Lookup Zone so that those links are valid.
    You can also look into using PHP to detect user IP.

  • #3 / Feb 22, 2010 1:54pm

    ParisJC

    150 posts

    Hi, lebisol.

    We can’t use logged in/out since we only require logins for some faculty and staff areas. Student use is open on the general site, and login for mail and other areas is on a different system than EE. The link(s) in question lead to those areas.

    I knew EE could provide the IP address of a user, but thought it was only if the user is logged in. However, I just ran a quick test, and EE provided the IP address when I was logged out. So I may be able to set up a simple conditional to provide the correct link:

    if ip_address == our C block
    —insert link A
    if:else
    —insert link B

    Or can ip_address be used as in a conditional?

  • #4 / Feb 22, 2010 2:08pm

    lebisol

    2234 posts

    Agh I figured but though to go the easy route first.
    Not sure about how EE collects IPs but my first guess would be to use query module and perhaps look into ‘exp_sessions’ or ‘exp_online_users’ tables.
    eg.

    {exp:query sql="SELECT ip_address FROM exp_online_users;"}
     
     {if {ip_address} != 'xxx.xxx.xxx.xxx'}
      Not on Campus
     {/if}
    
    {/exp:query}
  • #5 / Feb 22, 2010 3:11pm

    ParisJC

    150 posts

    Thanks for that tip. I’ll give that a whirl when I get back to the office in the morning.

  • #6 / Feb 22, 2010 4:21pm

    ender

    1644 posts

    since you want to check for a matching IP down to the C block and not an exact match you won’t be able to do this with a simple {if} condition tag.

    I’d give Mark’s Switchee plugin a try in this case, since it supports regular expressions now.

    {exp:switchee variable="{ip_address}" parse="inward"}
        {case value="#192\\.168\\.1\\.[0-9]{1,3}#"}
            <a href="http://whatever">asdf</a>
        {/case}
    
        {case default="Yes"}
            <a href="http://something_else">qwer</a>
        {/case}
    {/exp:switchee}

    also fwiw: the {ip_address} tag is a global variable so you don’t need to perform a separate query to get it.

  • #7 / Feb 23, 2010 10:35am

    ParisJC

    150 posts

    Hadn’t seen that. Thanks, ender!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases