Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Block IP with PHP Error

    Hello there!

    I would highly appreciate it if anybody could suggest anything as only half of the very simple code is working. I am totally stuck and feel silly as I think it is very simple but I just cannot see it... Here is the code
    [CODE]
    <?php
    $banned[0]="1.1.1.1";
    $banned[1]="2.2.2.2";
    $banned[2]="3.3.3.3";
    $banned[3]="4.4.4.4";
    if (in_array($_SERVER['REMOTE_ADDR'], $banned)) { include("noaccess.php");
    exit();
    }
    ?>
    [CODE]

    The problem is that it won't block / redirect the third IP address. The IP addresses shown above are fictional, but the ones I have are as real as they can only be. I have checked them for about 20 times and there are no spelling mistakes, no syntax mistakes and so on... However, the third IP addrress just won't go to noaccess.php no matter what... It starts slowly driving me crazy. I'd be really thankful for any advices, comments, suggestions, any help at all! Many thanks in advance!

  • #2
    New Coder
    Join Date
    Apr 2008
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts
    When i try this code, on a blank page my end, and replace an ip with my own, it works as intended. So no idea where it goes wrong, somewhere else maybe?

  • #3
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much for your feedback and for giving it a try Jilli.

    When I put different IP addresses including mine it also works. It's just this tricky IP...

    Well, the only idea that comes to my mind is that the server which IP fails simply does not have PHP installed... Server's misconfiguration? But in that case the other part of the code wouldn't work as well, but it works...

    When the IP in question accesses the page in question I have one extra line at the end that uses mail to email me access notification and it works (email is sent) but it never redirects to noaccess.php.

    Any more suggestions please, anybody?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Only thing I can think of is that its something to do with the character encoding. Although it looks the same on-screen it might be a different binary value in utf8 etc.

    How you'd test that is beyond me.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #5
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you tangoforce. It's at least something to have a look. However, I noticed one more thing which might be exactly the trouble-'causer' There were two pieces of code with the same variable name $banned.

    The first one was:

    [CODE]
    <?php
    $banned[0]="1.1.1.1";
    $banned[1]="2.2.2.2";
    $banned[2]="3.3.3.3";
    $banned[3]="4.4.4.4";
    if (in_array($_SERVER['REMOTE_ADDR'], $banned)) { include("noaccess.php");
    exit();
    }
    ?>
    [CODE]

    And the second one was:

    [CODE]
    <?php
    $banned[0]="5.5.5.5";
    $banned[1]="6.6.6.6";
    $banned[2]="7.7.7.7";
    $banned[3]="8.8.8.8";
    if (in_array($_SERVER['REMOTE_ADDR'], $banned)) { include("noaccess.php");
    exit();
    }
    ?>
    [CODE]

    The difference in these two were just different IP addresses. Then I remembered that PHP worked successively in series therefore the first $banned was already occupied by the first IP address. Then the second block of the same code also had the same $banned but since it was occupied earlier by the first line, second line etc. it did not work. Is that right? I thought that exit(); clears everything including stuff stored in $banned and the next block of the same code would have it all empty and the new IP addresses would be re-stored / re-assigned into the $banned again. Is that not correct?

    Looks like what I have to do is to re-name the $banned of the second same block of code into something else for example $blocked and I am done. Alternatively, I could just go on numbering my $banned so that there wouldn't be any identical / same entries, for example $banned[1], $banned[2] and so on as $banned[2] and $banned[2] even in different blocks of code won't work. Would appreciate it if anyone commented if I am on the right track here? Many thanks!

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    We can't comment on this really because you're only posting small sections of code and expecting us to see the bigger picture.

    IF you post the whole code, we might be able to help further.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #7
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks tangoforce, but that's all the code I have. I can post every single bit of it here of course but it won't be much longer. So here goes. Would be grateful for any further comments then. These two are separated because the first include and the second include redirect to different URLs.

    [CODE]
    <?php
    $banned[0]="204.11.99.122";
    //Add as many as you wish at $banned[1]= then $banned[2]= and so on. $banned[1]="122.99.11.204";
    $banned[2]="208.75.177.84";
    $banned[3]="207.14.32.240";
    if (in_array($_SERVER['REMOTE_ADDR'], $banned)) { include("noaccess.php");
    $datetime = gmdate('D, d M Y H:i:s',time()+7200) . ' LT TIME '; // 10800- initial setting
    $servertime = date(" G:i:s") . " server's time " ;
    $body = "Message here";
    $alldata = $datetime . "<p></p>" . $servertime . " - " . $ip . "<p></p>" . $body;
    $to = "email@address.com";
    $subject = "Spammers Visit!";
    $from = "email@address.com";
    $headers = "From: $from" . "\r\n";
    $headers .= "content-type: text/html; charset=utf-8" . "\r\n";
    mail($to, $subject, $alldata, $headers);
    exit();
    }
    ?>
    <?php
    $ip = "IP Address: ".$_SERVER['REMOTE_ADDR'];
    $banned[0]="78.61.105.46";
    $banned[1]="217.117.28.48";
    $banned[2]="195.14.181.254";
    $banned[3]="35.100.144.108";
    $banned[4]="38.85.61.251";
    if (in_array($_SERVER['REMOTE_ADDR'], $banned)) { include("nosaccess.php");
    $datetime = gmdate('D, d M Y H:i:s',time()+7200) . ' LT TIME '; // 10800- initial setting
    $servertime = date(" G:i:s") . " server's time " ;
    $body = "Message here";
    $alldata = $datetime . "<p></p>" . $servertime . " - " . $ip . "<p></p>" . $body;
    $to = "email@address.com";
    $subject = "Spammers Visit!";
    $from = "email@address.com";
    $headers = "From: $from" . "\r\n";
    $headers .= "content-type: text/html; charset=utf-8" . "\r\n";
    mail($to, $subject, $alldata, $headers);
    exit(); }
    ?>
    [CODE]

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Ok thats a bit messy. You need to remove the second set of IP addresses and put them in the first array of banned addresses. Then delete the second block of code.

    Try that, test and report back.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #9
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Ok thats a bit messy. You need to remove the second set of IP addresses and put them in the first array of banned addresses. Then delete the second block of code.

    Try that, test and report back.
    Messy? You made me laugh. It'working and it's as clean and as simple as can be. If I delete the other part then I might as well delete everything else and just beat it LOL! The solution to delete half of the code is the silliest ever! I have a better idea. It's to delete all the code at all! Thus, if there is no code at all there is no issues at all either! Boy, what a waste of time!

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by klaipedaville View Post
    Messy? You made me laugh. It'working and it's as clean and as simple as can be. If I delete the other part then I might as well delete everything else and just beat it LOL! The solution to delete half of the code is the silliest ever! I have a better idea. It's to delete all the code at all! Thus, if there is no code at all there is no issues at all either! Boy, what a waste of time!
    Wow.
    What are you talking about? Are these actually separate scripts of code or are these in a single script? I think you intend that these are in separate scripts, but they appear to be in single script.
    In any case, remove redundancy and promote reuse anyway. Create a function which only accepts variable input on what could be changed. Identify what can change and what is different, and use those for your input.
    As for messy, I can't tell if its just from not having code/php tags surrounding it, but there is no indentation.
    PHP Code:
    <?php
    $banned
    [0]="204.11.99.122";
    //Add as many as you wish at $banned[1]= then $banned[2]= and so on. $banned[1]="122.99.11.204";
    $banned[2]="208.75.177.84";
    $banned[3]="207.14.32.240";
    if (
    in_array($_SERVER['REMOTE_ADDR'], $banned))
    {
        include(
    "noaccess.php");
        
    $datetime gmdate('D, d M Y H:i:s',time()+7200) . ' LT TIME '// 10800- initial setting
        
    $servertime date(" G:i:s") . " server's time " ;
        
    $body "Message here";
        
    $alldata $datetime "<p></p>" $servertime " - " $ip "<p></p>" $body;
        
    $to "email@address.com";
        
    $subject "Spammers Visit!";
        
    $from "email@address.com";
        
    $headers "From: $from" "\r\n";
        
    $headers .= "content-type: text/html; charset=utf-8" "\r\n";
        
    mail($to$subject$alldata$headers);
        exit();
    }
    ?>
    <?php
    $ip 
    "IP Address: ".$_SERVER['REMOTE_ADDR'];
    $banned[0]="78.61.105.46";
    $banned[1]="217.117.28.48";
    $banned[2]="195.14.181.254";
    $banned[3]="35.100.144.108";
    $banned[4]="38.85.61.251";
    if (
    in_array($_SERVER['REMOTE_ADDR'], $banned))
    {
        include(
    "nosaccess.php");
        
    $datetime gmdate('D, d M Y H:i:s',time()+7200) . ' LT TIME '// 10800- initial setting
        
    $servertime date(" G:i:s") . " server's time " ;
        
    $body "Message here";
        
    $alldata $datetime "<p></p>" $servertime " - " $ip "<p></p>" $body;
        
    $to "email@address.com";
        
    $subject "Spammers Visit!";
        
    $from "email@address.com";
        
    $headers "From: $from" "\r\n";
        
    $headers .= "content-type: text/html; charset=utf-8" "\r\n";
        
    mail($to$subject$alldata$headers);
        exit();
    }
    ?>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by klaipedaville View Post
    Messy? You made me laugh.
    Good!

    Quote Originally Posted by klaipedaville View Post
    It'working and it's as clean and as simple as can be.
    But it is not working - you've said so in your opening post. You yourself then theorised that this may be because of the second block of code. How can you say it's working and is clean?

    Quote Originally Posted by klaipedaville View Post
    If I delete the other part then I might as well delete everything else and just beat it LOL!
    Why do you need the same code repeated in the script? It seems a bit pointless.

    Put all your banned IP addresses in the array in the first block of banning code and be done with it.


    Quote Originally Posted by klaipedaville View Post
    The solution to delete half of the code is the silliest ever!
    In your opinion. You're the one asking for help, you're the one who has suggested that your second bit of almost identical code may be the problem and the only way to test that is to remove it. Is testing something to see if it solves the problem silly?

    Quote Originally Posted by klaipedaville View Post
    I have a better idea. It's to delete all the code at all!
    I've tried to help you here. You can only truly work out if that second segment of code is causing a problem (as you theorised) by removing it.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  

    Posting Permissions

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