There's several ways to do this, since you have the htaccess
tag in your question, I assume you're looking for an htaccess file solution.
If you're not using apache 2.4 (or higher), you can use mod_authz to handle all of the blocking. Something like this:
# this makes it so blocked IPs get shown the block.php page
ErrorDocument 403 /block.php
Order Allow,Deny
Deny from all
Allow from 1.2.3.4
Allow from 1.2.3.5
Allow from 5.6.7.8
etc...
Here, you block everything and keep a list of "Allow from" lines. You can shorten them if you want to allow an entire subnet, e.g.
Allow from 1.2.3
will allow an IP starting with 1.2.3.
You can also use mod_rewrite for this:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.5$
RewriteCond %{REMOTE_ADDR} !^5\.6\.7\.8$
RewriteRule ^ /block.php [L,F]
Here, you have a bit more flexibility since you can use a regular expression to match the IP. So you can do stuff like:
RewriteCond %{REMOTE_ADDR} !^1\.2\.[3-6]\.
So this allows all IPs starting with: 1.2.3, 1.2.4, 1.2.5, and 1.2.6.
You can also put these directives into the server/vhost config file, it'll work a bit faster there instead of the htaccess file.