• home
  • forum
  • my
  • kt
  • download
  • PHP/mySQL Simple IP Banning

    Author: 2008-08-16 12:33:40 From:

    Today I am going to show you how to create an IP ban system. This does not include an IP manager, although if I receive enough requests, I will.

    To start things off we need to create the page conf.inc.php. This page will be included in all the pages and contain the ban function and database details.

    1. <?php
    2. $db_user = ""; // Username
    3. $db_pass = ""; // Password
    4. $db_database = ""; // Database Name
    5. $db_host = ""; // Server Hostname
    6. $db_connect = mysql_connect ($db_host, $db_user, $db_pass); // Connects the the database
    7. $db_select = mysql_select_db ($db_database); // Selects the DB we will be searching in.
    8.  
    9. // IP Ban Check
    10. function is_banned($ip) { // Starts the is_banned function.
    11.         $q = mysql_query("SELECT * FROM `ban` WHERE `ip` = ‘$ip’") or die(mysql_error()); // Searches the database for the users ip.
    12.         $rows = mysql_num_rows($q); // Counts all the results found.
    13.                
    14.         if ($rows > 0) { // If more than 0 were found.
    15.                 $banned = true; // The user is banned.
    16.         } else {
    17.                 $banned = false; // The user is not banned.
    18.         }
    19.        
    20.         return $banned;
    21. }
    22. ?>

    Breakdown:
    We 1st connect the the database in order to retrieve data.
    Then we create the function called is_banned() to check if the users ip is in the ban list.

    Now we will implement this code in index.php.

    1. <?php
    2. include("conf.inc.php"); // Include the database and function details.
    3. if (is_banned($_SERVER["REMOTE_ADDR"])) { // Start out function with the users ip.
    4.         echo "Error: You have been banned from this website!"; // The user is banned.
    5.         exit(); // Halts the rest of the script from performing.
    6. } else {
    7.         echo "You have not been banned, congrats on not being one of the many assholes.";
    8. }
    9. ?>

    Now that script is not complex, but it is so you can build on it. I hope this was helpful to you. As always, if you have any questions, please feel free to ask.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (27)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot