SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    West Columbia, SC
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirect based on IP of user

    We have a redirect page that we need to be able to direct internal users on our private network one way and public users to another page.

    Is there a way with javascript to detect the ip and if it is 10.10.* then go to x page?

    Thanks for the help!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    San Francisco
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pgowder
    We have a redirect page that we need to be able to direct internal users on our private network one way and public users to another page.

    Is there a way with javascript to detect the ip and if it is 10.10.* then go to x page?

    Thanks for the help!
    I believe if you use an .shtml extension for your document (for some servers), you would be able to retrieve server vars - try this:

    var ip = '<!--#echo var="REMOTE_ADDR"-->';

    if(ip.search(/10.10/) >= 0) { document.location = 'http://www.sitepoint.com'; }

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    San Francisco
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pgowder
    We have a redirect page that we need to be able to direct internal users on our private network one way and public users to another page.

    Is there a way with javascript to detect the ip and if it is 10.10.* then go to x page?

    Thanks for the help!

    Sorry - this probably answers your question better:

    var ip = '<!--#echo var="REMOTE_ADDR"-->';

    document.location = (ip.search(/10.10/) >= 0) ? 'www.internalsite' : 'www.publicsite.com';

  4. #4
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    West Columbia, SC
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samgrinter
    Sorry - this probably answers your question better:

    var ip = '<!--#echo var="REMOTE_ADDR"-->';

    document.location = (ip.search(/10.10/) >= 0) ? 'www.internalsite' : 'www.publicsite.com';
    And this is enclosed between <javascript> tags?

  5. #5
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    West Columbia, SC
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what I have:


    HTML Code:
    <script language="javascript">
    var ip = '<!--#echo var="REMOTE_ADDR"-->';
    document.location = (ip.search(/10.10/) >= 0) ? 'http://www.yahoo.com' : 'http://intranet';
    </script>
    And testing it inside or out sends you to the same place??

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    San Francisco
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pgowder
    Here's what I have:


    HTML Code:
    <script language="javascript">
    var ip = '<!--#echo var="REMOTE_ADDR"-->';
    document.location = (ip.search(/10.10/) >= 0) ? 'http://www.yahoo.com' : 'http://intranet';
    </script>
    And testing it inside or out sends you to the same place??
    Hi -

    Yes, sorry - it should be between <script> or <script language='javascript'> tags.
    Here is the full script:

    <script>
    <!--

    var ip = '<!--#echo var="REMOTE_ADDR"-->';

    document.location = (ip.search(/10.10/) >= 0) ? 'www.intranet' : 'www.yahoo.com';

    //-->
    </script>

    Depending on what server you are running from, you may need to use a .shtml extension for your script document. It should work from inside and out because the page is being called form the same server and this is serving as an entry point. Let me know how it goes-

    -samgrinter

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    samgrinter...

    document.location ----> window[self/top/parent].location
    (ip.search(/10.10/ ----> (ip.search(/10\.10/
    <script language='javascript'> ----> <script type="text/javascript">
    ::: certified wild guess :::

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    San Francisco
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks adios - good to know.

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just theorizing here

    instead of
    Code:
    ip.search(/10\.10\./)  == 0
    just use
    Code:
    /^10\.10\./.test( ip )
    Should be faster. Better yet, the fastest operation is probably
    Code:
    ip.indexOf( '10.10.' ) == 0
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •