www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to create list ip

  1. #1
    Join Date
    May 2009
    Posts
    12

    How to create list ip

    I have problem for this script.
    I want to create list ip for acceptable.
    Example.
    PHP Code:
    <?php
    $ip 
    = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    if(
    $ip=="IP FOR ACCEPT HERE")
    {
    //Code after ip accept here.
    }else{
    echo
    "Sorry $ip Not allowed!";
    }
    ?>
    Or use fopen for text file ip.txt
    But i'm unknown fopen.

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    Assuming your file is like this:

    Code:
    123.123.123.123
    112.112.112.112
    123.123.123.123
    231.231.231.231
    etc...
    The following should work:

    PHP Code:
    <?php

    $ip 
    = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    $whitelist file('./ip.txt');

    if (
    in_array($ip$whitelist)) {
        
    // accept
    } else {
        
    // reject
    }

    ?>

  3. #3
    Join Date
    May 2009
    Posts
    12
    This also could?
    PHP Code:
    $whitelist file('./ip.txt');
    if(
    $ip $whitelist
    Thanks for help,

  4. #4
    Join Date
    Oct 2005
    Posts
    843
    Quote Originally Posted by waletmp3 View Post
    This also could?
    PHP Code:
    $whitelist file('./ip.txt');
    if(
    $ip $whitelist
    Thanks for help,
    Don't believe that would work due to you would be comparing to see if there IP is greater than the list of acceptable IP when their IP could be above or below something(not to mention an IP isnt a straight number it has periods in it).

    Unless you are doing something like a range of IP, but comparing it to an array like that wouldn't work. (or at least to my knowledge) you would have to compare it each array value individually. Mindzai solution is the best to check if their ip is in the array using that in_array function to either get TRUE or FALSE back.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles