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 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP/Mysql/IP block

    Hi everyone,
    I have got a script at the top of my index page that blocks IP addresses and this successfully works. But now i am trying to store the IP's in a mysql database so i can update/add new ones from an admin section on my website. here is the code i have come up with. but it doesnt work

    PHP Code:
    <?php
    mysql_connect
    ("localhost""user""pass") or die(mysql_error()); 
    mysql_select_db("db") or die(mysql_error()); 
    $query mysql_query("SELECT * FROM `adminip` WHERE id='1'");
    $row mysql_fetch_array($query);

    $deny = array( $row[content]);
    if (
    in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
       echo(
    "You have been blocked from --mywebsite--. if an error has occured, please fill out the following form.");
       exit();
    }
    ?>
    Where i put $row[content] is where i want the listed ip's to appear.
    The database looks like this..
    Code:
    "111.111.111", "222.222.222", "333.333.333"
    all help appreciated!

    James.
    woooooo!

    visit my website:
    click here!

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Does that mean the database looked like this?

    Code:
    id           content
    1            "111.111.111.111","222.222........"
    If not, the above code won't work. Paste more of the structure of the database. Censor any sensitive information, but at least give all the info.

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yep sorry thats what it looks like with only the one row

    Code:
    id           content
    1            "111.111.111.111", "222.222.222.222", "333.333.333.333"
    James.
    woooooo!

    visit my website:
    click here!

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The only thing I can think of then is the "" are being included. Try either removing them from the database or adding them to the comparison eg:

    PHP Code:
    in_array ('"'.$_SERVER['REMOTE_ADDR'].'"'$deny)) { 

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,024
    Thanks
    2
    Thanked 314 Times in 306 Posts
    Echo $deny to see what it actually contains.

    The point of using a database would be to make your code general purpose and simplier. By storing a list of ip addresses in one single row, you are now totally dependent on the format of that row being correct. I recommend storing each IP address in its own row. This will also allow you to perform the comparison in the query instead of needing to fetch the row from the database, put it into an array and use PHP to perform the comparison.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    New Coder
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When i echo $deny it simply comes out as
    Code:
    Array
    thats with the " in the database and i get the same result if i put the " in the code as you suggested.

    How would i go about putting each ip into a new line?
    woooooo!

    visit my website:
    click here!

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,024
    Thanks
    2
    Thanked 314 Times in 306 Posts
    Upon further review. Your code that sets $deny won't work as is anyway. It sets a single array element with what came from the database, instead of separate elements with each separate value in the string that came from the database.

    To make a new row for each IP address, just do an INSERT query with the value from your form. You are probably doing an UPDATE now (read the existing row, display it on your form, modify it in the form field, submit the form, run an UPDATE query.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It will work if he keeps his database as it is (well, should work).

    I agree though, changing the database is a much better option. As you have it now, it is as good as a text file. Except that no-one can direct link to it.

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,024
    Thanks
    2
    Thanked 314 Times in 306 Posts
    You cannot directly put a variable with coma-separated values into an array and create separate array elements. It would require that the string be in the correct format to start with and explode it to produce the array elements.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Even if you were going to store all of the IPs in one column you don't need the quotes around the IPs.


  •  

    Posting Permissions

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