/    Sign up×
Community /Pin to ProfileBookmark

script read blocklist ips and restrict page

Hi
This php code try to read Ips from blacklist.txt file and then restrict access to page from visitors with same IP in blacklist.txt

[CODE]<?php
if ( !file_exists(‘blacklist.txt’) ) {
$deny_ips = array(
‘127.0.0.1’,
‘192.168.1.1’,
‘83.76.27.9’,
‘192.168.1.163’
);
} else {
$deny_ips = file(‘blacklist.txt’);
}
// read user ip adress:
$ip = isset($_SERVER[‘REMOTE_ADDR’]) ? trim($_SERVER[‘REMOTE_ADDR’]) : ”;

// search current IP in $deny_ips array
if ( (array_search($ip, $deny_ips))!== FALSE ) {
// address is blocked:
echo ‘Your IP adress (‘.$ip.’) was blocked!’;
exit;
}
?>[/CODE]

But
-This code can’t read blacklist.txt if it has been tons thousands IP listed , how can fix it and make it available to read big list
and
– How can i make script to read Ips from an external URL for get low load in CPU . for example blacklist be on another server and address :[url]http://domain2.com/blacklist.txt[/url]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 02.2017 — It will be much more efficient if you import the list into a database. Then if you index the IP column, searches will be [i]much[/i] faster than trying to sift through a large text file.
Copy linkTweet thisAlerts:
@billmauthorJan 02.2017 — what code should be there to read from database
Copy linkTweet thisAlerts:
@rootJan 02.2017 — what code should be there to read from database[/QUOTE]

That all depends on your database table.

What have to created as a set of fields for a table in a database? It helps people to know what it is that you have set up.
Copy linkTweet thisAlerts:
@billmauthorJan 02.2017 — table has been a field for IP : IP_ad

script read from that table , if IP be in it , then restrict page
Copy linkTweet thisAlerts:
@NogDogJan 02.2017 — Well, one approach:
[code=php]
$pdo = new PDO($dsn, $user, $pwd); // read the documentation
$stmt = $pdo->prepare("SELECT COUNT(*) FROM table_name WHER IP_ad = :ip");
$stmt->bind_param(':ip', $ip_address);
$stmt->execute();
if($stmt->fetchColumn() > 0) {
// match found
}
[/code]
Copy linkTweet thisAlerts:
@billmauthorJan 02.2017 — thanks

I want if IP be in list php code doesnt permit page be loaded and shows 'no' message

and if visitor IP be in list , php code lets page be loaded as normal
Copy linkTweet thisAlerts:
@rootJan 02.2017 — Your IP table really should have an expire flag which will either be a timestamp when the block expires or a flag to indicate that it never expires.

You will need to look at two possible routes, one being that the user logs in and is verified as being a member of the site by a database login check and then an IP address is recorded and stored in a session, then each page load checks if a session is set and if the IP address matches the one PHP is given and then you can determine whether to log and block or to allow output.


The session option is better because it means you can vary the session lengths to force the user to login and if the ip address is not recognised they can be forced to log in and if they fail log in you can block the user after 5 attempts for a period of time.
Copy linkTweet thisAlerts:
@billmauthorJan 03.2017 — I want ban proxy IPs to view the page

so there is not a season , If a visitor load page with same IP in the table so he/she will see 'no' message on the page always
Copy linkTweet thisAlerts:
@rootJan 03.2017 — I want ban proxy IPs to view the page

so there is not a season , If a visitor load page with same IP in the table so he/she will see 'no' message on the page always[/QUOTE]


Season? Its a SESSION and they are available in PHP to allow building of sites that need to control access.

and banning proxy servers is just plain stupid given most of the internet is run through proxy servers, you do want visitors to your site don't you?

If you are operating a block likst then you would only run one for persistent connections or connections that are identified as creating hot links. What you do is monitor resource requests by way of using server rewrites and that is done with the .htaccess file in the server root.

What you want is your page to have been requested and the request for a resource verified as coming from a page that you have issued, for that you use sessions and a tokens system.
×

Success!

Help @billm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...