Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Block User From Visiting Website Using PHP

Thursday, Sep 28, 2017

Tags:- PHP MySQL HTML


Sometimes there are some user in a website who try to break website policies, not doing things as per website guidelines, post malicious data in website, put comment on website that harm website image then website owner's take some serious action against those user by getting there ip address and temporary or permanently block user from visiting website. So, in this tutorial we will show you how to block user from visiting your website using PHP.You may also like get visitor details using PHP.


Block User From Visiting Website Using PHP


To Block User From Visiting Website It Takes Only One Step:-

  1. Make a PHP file to block user from visiting website


Step 1.Make a PHP file to block user from visiting website

We make a PHP file and save it with a name block.php

// Database Structure 
CREATE TABLE 'blocked_user' (
 'ip' text NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php

 $host="localhost";
 $username="root";
 $password="";
 $databasename="sample";
 $connect=mysql_connect($host,$username,$password);
 $db=mysql_select_db($databasename);

 $user_ip=$_SERVER['REMOTE_ADDR'];
	
 $get_result = mysql_query("select * from blocked_user where ip='$user_ip'");
 if(mysql_num_rows($get_result)>0)
 {
  echo "<h1>Your Are Blocked From Visiting This Website</h1>";
 }
 exit();
 
?>
 
<html>
<body>
<div id="wrapper">
<h1>Block User From Visiting Your Website Using PHP</h1>

<div>
 Your Website Content
</div>

</div>
</body>
</html>

In this step we create a database table called 'blocked_user' to store ip address of blocked user and then we connect to database and and get the ip of user when user visit website and check if user ip is present in our database if ip is present then we display the block message and hide rest of the content and if user ip is not present it means the user is not blocked then we display website as usual.You may also like get user location using ip address.


That's all, this is how to block user from visiting website using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me


Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.