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.
- <?php
- $db_user = ""; // Username
- $db_pass = ""; // Password
- $db_database = ""; // Database Name
- $db_host = ""; // Server Hostname
- $db_connect = mysql_connect ($db_host, $db_user, $db_pass); // Connects the the database
- $db_select = mysql_select_db ($db_database); // Selects the DB we will be searching in.
- // IP Ban Check
- function is_banned($ip) { // Starts the is_banned function.
- $q = mysql_query("SELECT * FROM `ban` WHERE `ip` = ‘$ip’") or die(mysql_error()); // Searches the database for the users ip.
- $rows = mysql_num_rows($q); // Counts all the results found.
- if ($rows > 0) { // If more than 0 were found.
- $banned = true; // The user is banned.
- } else {
- $banned = false; // The user is not banned.
- }
- return $banned;
- }
- ?>
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.
- <?php
- include("conf.inc.php"); // Include the database and function details.
- if (is_banned($_SERVER["REMOTE_ADDR"])) { // Start out function with the users ip.
- echo "Error: You have been banned from this website!"; // The user is banned.
- exit(); // Halts the rest of the script from performing.
- } else {
- echo "You have not been banned, congrats on not being one of the many assholes.";
- }
- ?>
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
