Jump to content
xisto Community
cwconline

Ip Blocking Php Script! Stop the bad guys before they stop you.

Recommended Posts

Hello, I did some search's to see if anyone had covered this apparently not! So ok! Lets set up a Ip Site blocker using php and mysqlThe First thing we are going to do is make the form that is actually blocking the site.Note: You Must have this in a PHP file to work, it must be at the VERY TOP!!!!!

<?php$vip = $_SERVER['REMOTE_ADDR'];include "config.php";############################### See if the Ip is in the blocked list               ###############################$get_info = mysql_query("SELECT * FROM blocked WHERE ip = '$vip'");$do_block = mysql_fetch_array($get_info);#############################################  Now we are just checking to see if we need to block the user      #############################################         if( $do_block['ip'] == "$vip" ){        die("Sorry! Your Ip has been blocked from viewing our sites content");} ?>

The above just save it however you want to... I would recommend ipblocklist.phpbefore that works we need to create the table inside of a database that will have the ip's stored inside of themSo in phpMyAdmin or your MySql Query Window type in

USE 'databasename';

Excute it...

CREATE TABLE blocked( id int not null auto_increment primary key,ip varchar(15),added datetime);

Excute...Now you are set up! Ok, Good Job on the MySql Database set up...Now, we need to create a config file which will store how we are connecting and what database we are using

<?php##########################    Config File                                 ##########################$dbhost = "localhost"; // This is your database host name usally localhost$dbuser = "user"; // This is your database user name...$dbpass = "pass"; // this is your password used to login to the database$dbname = "database"; // this is your database name.########################### Edit the above to suit your needs    ############################  $conn = mysql_connect("$dbhost","$dbuser","$dbpass");               mysql_select_db("$dbname", $conn );?>

Then Your Done...I would save this as config.php because thats what we imported into our ip checker file(e.g. inlcude "config.php"; )Ok, as I said you need to put this line of code on your first line after <?php or if you are using session_start();after thatit's should like this

<?phpinclude "ipblocklist.php";// The rest of your code?>

Now, it's time to make a admin type area so you can add or remove blocked Ip's...

<?phpsession_start();###################### Get the Config File               ######################include "config.php";########################## Set the admin credentials              # #########################$admin = 'admin';$pass = 'password';$s_user = $_POST["admin"];$s_pass = $_POST["pass"];if( $ses_user <= " " ){########################## Admin Login Form                         ##########################echo "<form action='ipadmin.php' method='post'>";echo "Admin: <input type='text' name='admin'><br>";echo "Pass: <input type='password' name='pass'><br>";echo "<input type='submit' value='Login'>";echo "</form>";}    if( $s_user == $admin and $s_pass == $pass ){         $ses_user = $_SESSION['admin'] = $s_pass;        $go = 1; }if( $go = 1 ){echo "Add a Ip to the Blocked List";echo "<form action='ipadmin.php' method='post'>Ip: <input type='text' name='ip'><input type=submit value='Block Ip'></form>";}if( $_POST["ip"] > " " ){$ip2 = $_POST["ip"];echo "Are you sure you want to block $ip2";echo "<form action='ipadmin.php' method='post'><input type='hidden' name='ip2' value='$ip2'><input type='submit' name='block' value='yes'>";}if( $_POST["block"] == "yes" ){  mysql_query("INSERT INTO blocked SET ip = '$ip2' added = NOW()") or die("Could not Block Ip, MySql Error");}echo "<center>Remove Blocked Ips<br><br>";echo "<form action='ipadmin.php' method='post'><select name='remove'><option value=''>Select One</option>";$ips = mysql_query("SELECT * FROM blocked");while( $showips = mysql_fetch_array($ips)){$ip_value = $showips['ip'];echo "<option value='$ip_value'>$ip_value</option>";}echo "<input type='submit' value='RemoveIp' name='Remove Ip'>";echo "</form>";if( $_POST["Remove Ip"] == "RemoveIp" ){$del_ip = $_POST['remove'];mysql_query("DELETE FROM blocked WHERE ip = '$del_ip' LIMIT 1");echo "The Ip $del_ip has been removed from the blocked list!";}?>

OK now save that above as ipadmin.phpEverything Should work succesfully any question

Share this post


Link to post
Share on other sites

Yeah, I mostly did this for people who does not have a Ip Blocker installed on there machine... It's basicly the same way but somethings, are diffrent...

Share this post


Link to post
Share on other sites

I am able to get an IP blocked using this script, but the ipadmin.php is giving an error when adding or removing ip's.I have a good db connection, since it blocks the IP on my localhost, but I had to add the ip using phpadmin in order to acquire the block. Any suggestions? Is there a working link for this script available?

Share this post


Link to post
Share on other sites

Hello Admin,I have messaged you via feedback form..i like what code you have above..first of all i am not able to add the ip address.. or remove the ipaddress from ipadmin.phplets seei set up everytihng correct setup database also... nowproblem is.....in user and pass... i enter..... admin as user and password as password.but this take me no where and same page stays... still those two fields of user and password.. remain on their..now i have to add ip .. lets say i add ip 127.0.0.1 for example.... nd say Yes.. or click yes.. the i recieve this error Could not Block Ip, MySql Errorhence.. this is the issue.. and now what i did i went to mysql db... and inserted manuly.... ip and time....then when i go to ipadmin.php.. i can see that ip 127.0.0.1 now lets say i click remove.. nothing hppn.. ip still der.. and remains..Please can you again check the issue.. or improve same script and let me know.. the fix.. I really need urgent...thank you,hero

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.