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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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.
1 2 3 4 5 6 7 8 9 | <?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.
Hi, today I am going to show you how to make a simple membership system. This included, registering for an account, logging in, security for pages, and logging out.
Now shall we begin? I say yes!
Our database will be setup like the following:
1 2 3 4 5 6 7 | CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(11) NOT NULL auto_increment, `username` varchar(225) NOT NULL default '', `password` varchar(225) NOT NULL default '', `email` varchar(225) NOT NULL default '', PRIMARY KEY (`user_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; |
Breakdown:
user_id is the default value that keeps track of users.
username is the users log in name.
password is the users log in password.
email is the users email, so in later versions of the member system, a forgot password can be added.
Our 1st bit of code will be a file named conf.inc.php. This file holds all of our mysql and function data, so we don’t have to enter it over and over
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < ?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 to the database. $db_select = mysql_select_db ($db_database); // Selects the database. function form($data) { // Prevents SQL Injection global $db_connect; $data = ereg_replace("[\'\")(;|`,<>]", "", $data); $data = mysql_real_escape_string(trim($data), $db_connect); return stripslashes($data); } ?> |
Breakdown:
The 1st part is all the mySQL information in order to view and insert data.
The 2nd part prevents SQL injection, so people cant gain unauthorized access.
(more…)