Only allow users on page if IP address is approved

54
May 19, 2019, at 12:30 PM

How can I make an HTML (and CSS/PHP/JavaScript) document which only allows certain IP addresses on a page?

(I am not asking how to find IP address with PHP, but how to allow access to a page based on IP.)

Answer 1

Try this with PHP :

function CheckIPAccess() {
  //allowed IP. Change it to the IP addresses you want to allow to access your webpage
  $allowedip = '127.0.0.1';
  $ip = $_SERVER['REMOTE_ADDR'];
  return ($ip == $allowedip);
}
Answer 2

put this on the top of your php file and update the allowedIps variable with the IPs that you want to allow.

$allowedIps = ['198.x.x.x', '200.x.x.x'];
$userIp = $_SERVER['REMOTE_ADDR'];
if (!in_array($userIp, $allowedIps)) {
    exit('Unauthorized');
}

for non PHP files (eg .html, .css) you will have to update your .htaccess file to add file specific permission. The following SOF thread should help: (assuming you are using apache server)

.htaccess: how to restrict access to a single file by IP?

Answer 3

If you dont want to bother with the code, put your site on Cloudflare and block ips

READ ALSO
What is the proper Query and PHP to print an archive of the database based on Year, Month?

What is the proper Query and PHP to print an archive of the database based on Year, Month?

I've found plenty of examples that either don't show the query, or don't show the codeI'm looking to format the output as such:

29
control structures & functions- filter function [on hold]

control structures & functions- filter function [on hold]

You are given a list of strings such as

42
Native Android Application for website without WebView

Native Android Application for website without WebView

I need to build a Native Android Application for a Website that I have successfully developedHow do I go about this without using the webView

42