How to add 19 more states to ip block.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • starlight2200
    New Member
    • Jan 2010
    • 2

    How to add 19 more states to ip block.

    I'm using a web statistic service and I want to only allow certain states to my site as a testing mode for a certain period of time and block all non US countries.

    I received the correct javascript below, but I can't figure out how to add the addtional states to the else statement. I tried everything. I don't know javascript. I need to add 19 more states to the else statement correctly. Can you give me the correct sequence to follow by adding 4 or five states as sampe to the else statement and letting me know how to end the statement correctly? The script below does work.

    Code:
    <script language="JavaScript">
    var geo = disp();
    if (geo[0] != 'US'){
       alert('Sorry this site is only accessible from the USA');
    
       window.location = ('http://www.yahoo.com'); 
    }
    else {
       if (geo[2] != 'CA' && geo[2] != 'TX' ){ (NEED TO ADD STATES HERE)
          alert('Sorry this site is only accessible from California and Texas');
    
          window.location = ('http://www.yahoo.com'); 
       }
    }
    </script>
    Last edited by Dormilich; Jan 11 '10, 09:34 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I recommend a switch statement
    Code:
    switch (geo[2])
    {
        case: "CA":
        case: "TX":
        case: "WA":
        // …
        case: "TN":
            // execute "valid" code
            break;
        default:
            alert(…);
            location.href = "…";
    }

    Comment

    • starlight2200
      New Member
      • Jan 2010
      • 2

      #3
      That was fast thanks.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you get 5 extra dice if you’re a moderator ;)

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          another idea to shorten the code a bit would be to use a 'hash-map' like this:
          Code:
          var statesMap = { 'CA': 1, 'TX': 1, 'WA': 1, ... };
          
          if ( geo[2] in statesMap) {
              // valid handling
          } else {
              // invalid handling
          }
          kind regards

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            and just another one using arrays:
            Code:
            var statesMap = ["WA", "TX", "CA", …];
            if (-1 == statesMap.indexOf(geo[2]))
            {
                // invalid handling
            }
            else
            {
                // valid handling
            }
            even searching in a string were possible …

            you see, there are many ways to do this

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Note: Poopy IE6 doesn't have the .indexOf() operator on its arrays.

              Comment

              Working...