SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Is it possible to have a static IP address included in this ?

    Is there any way of adjusting the code below, to also allow a static IP that isn't within the allowed countries please?

    Code:
    $config['redirect_to']='http://www.websiteaddress.com/';		//redirection url for visitors from not allowed countries
    $config['allowed_countries']='at,be,ch,de,dk,es,eu,fi,fr,gb,gg,gi,gr,ie,im,it,je,my,nl,no,pt,se,tr,tz,us';				//allowed countries codes
    Any help much appreciated.

    Dez

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are several options, but it really depends on how you're using the $config['allowed_countries'] variable. Are you just exploding it and then comparing it to something grabbed from $_SERVER, for instance? You could always just add the IP to the end of that list and use an extra condition in your check:

    Code:
    // assumed $visitorCountry and $allowedStaticIps are set already
    $allowed = explode(',', $config['allowed_countries']);
    
    if (!in_array($visitorCountry, $allowed) && !in_array($allowedStaticIps, $allowed)) {
      header("Location: $config['redirect_to']");
    }
    But that could get messy depending on how you extend this further. Personally I would just have the static IPs saved in a separate variable and do a check there too.

  3. #3
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks LeonardChallis. It might only get a max of 3-4 IP's to add. If it were you, how would you do it please?

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd do something like this:

    Code:
    /* presuming this is how the $config variable would end up - not necessarily how you'd create it */
    $config = array(
      'allowed_countries' => array('at', 'be', 'ch', 'de', 'dk', 'es', 'etc'),
      'allowed_ips' => array('192.168.0.1', '12.34.56.78', 'etc'),
      'redirect' => 'http://www.byebye.com/'
    );
    
    /* grab their details */
    $user = array(
      'ip' => $_SERVER['REMOTE_ADDR'],
      'country' => /* not sure what you're doing here, maybe something like... */ substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)
    );
    
    if (!in_array($user['country'], $config['allowed_countries']) && !in_array($user['ip'], $config['allowed_ips'])) {
      header('Location: ' . $config['redirect']);
    }
    
    echo "Welcome back my globally superior friends! ;)";
    This is a tad more long winded than it has to be, but will be better for readability and extensibility in my opinion.

    Good luck!


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
  •