All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Block User From Visiting Website Using PHP

Last Updated : Jul 1, 2023

IN - PHP MySQL HTML | Written & Updated By - Dikshita

In this tutorial we will show you how to block user from visiting your website using PHP, 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. 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.

I hope this tutorial on block visitor using php helps you and the steps and method mentioned above are easy to follow and implement.