· 8 years ago · Feb 17, 2018, 09:54 PM
1.htaccess file enabled = true
2create a temporary variable ($ip) in .htaccess file
3On user access to any .php pages in /test/ folder,
4retrieve user's ip and temporarily store in $ip variable.
5
6open connection on .php page
7load login table having ip match as $ip.
8if found then redirect
9else continue to loading page
10end if
11close connection
12end on
13destroy the variable
14
15<?php
16
17
18// Get the IP address of the visitor so we can work with it later.
19$ip = $_SERVER['REMOTE_ADDR'];
20
21// This is where we pull the file and location of the htaccess file. If it's in
22// the same directory as this php file, just leave it as is.
23$htaccess = '.htaccess';
24
25// This pulls the current contents of your htaccess file so we can search it later.
26$contents = file_get_contents($htaccess, TRUE)
27 OR exit('Unable to open .htaccess');
28
29// Lets search the htaccess file to see if there is already a ban in place.
30$exists = !stripos($contents, 'deny from ' . $ip . "n")
31 OR exit('Already banned, nothing to do here.');
32
33// Here we just pull some details we can use later.
34$date = date('Y-m-d H:i:s');
35$uri = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
36$agent = htmlspecialchars($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES);
37$agent = str_replace(array("n", "r"), '', $agent);
38
39// If you would like to be emailed everytime a ban happens, put your email
40// INSIDE the quotes below. (e.g. 'my@email.com')
41$email = '';
42
43// This is where we can whitelist IP's so they can never be banned. Simply remove
44// the // from the front of one of the example IP addresses below and add the
45// address you wish to whitelist. Make sure that you leave the single quotes (')
46// intact and the comma at the end. Adding a person to the whitelist AFTER they
47// have been banned will NOT remove them. You must open the htaccess file and
48// locate their ban by hand and remove it.
49$whitelist = array(
50 // '123.123.123.123',
51 // '123.123.123.123',
52 // '123.123.123.123',
53);
54
55
56// This section prevents people from being sent to this script by mistake
57// via a link, image, or other referer source. If you don't want to check
58// the referer, you can remove the following line. Make sure you also
59// remove the ending } at the very end of this script.
60if (empty($_SERVER['HTTP_REFERER'])) {
61
62// This section will write the IP address to the htaccess file and in turn
63// ban the address. It will however check the whitelist above to see if
64// should be banned.
65 if (in_array($ip, $whitelist)) {
66
67 // User is in whitelist, print a message and end script.
68 echo "Hello user! Because your IP address ({$ip}) is in our whitelist,
69 you were not banned for attempting to visit this page. End of line.";
70
71 } else {
72
73 // User is NOT in whitelist - we need to ban em...
74 $ban = "n# The IP below was banned on $date for trying to access {$uri}n";
75 $ban .= "# Agent: {$agent}n";
76 $ban .= "Deny from {$ip}n";
77
78 file_put_contents($htaccess, $ban, FILE_APPEND)
79 OR exit('Cannot append rule to .htaccess');
80
81 // Send email if address is specified
82 if (!empty($email)) {
83 $message = "IP Address: {$ip}n";
84 $message .= "Date/Time: {$date}n";
85 $message .= "User Agent: {$agent}n";
86 $message .= "URL: {$uri}";
87
88 mail($email, 'Website Auto Ban: ' . $ip, $message);
89 }
90
91 // Send 403 header to browser and print HTML page
92 header('HTTP/1.1 403 Forbidden', TRUE);
93 echo '<html><head><title>Error 403 - Banned</title></head><body>
94 <center><h1>Error 403 - Forbidden</h1>Hello user, you have been
95 banned from accessing our site. If you feel this ban was a mistake,
96 please contact the website administrator to have it removed.<br />
97 <em>IP Address: '.$ip.'</em></center></body></html>';
98
99 }
100
101}
102
103<FilesMatch 403.shtml>
104Order Allow,Deny
105Allow From All
106</FilesMatch>
107
108RewriteMap access txt:/path/to/blacklist.txt
109
110#Identify the ip correctly since it can be confusing.
111 $ipaddress = '';
112 if (getenv('HTTP_CLIENT_IP'))
113 $ipaddress = getenv('HTTP_CLIENT_IP');
114 else if(getenv('HTTP_X_FORWARDED_FOR'))
115 $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
116 else if(getenv('HTTP_X_FORWARDED'))
117 $ipaddress = getenv('HTTP_X_FORWARDED');
118 else if(getenv('HTTP_FORWARDED_FOR'))
119 $ipaddress = getenv('HTTP_FORWARDED_FOR');
120 else if(getenv('HTTP_FORWARDED'))
121 $ipaddress = getenv('HTTP_FORWARDED');
122 else if(getenv('REMOTE_ADDR'))
123 $ipaddress = getenv('REMOTE_ADDR');
124 else
125 $ipaddress = 'UNKNOWN';
126
127//WRITE THE IP TO HTACCESS TO BAN THE LOGIN FAILING IP
128 $file = fopen(".htaccess", "a+");
129 fwrite($file, "deny from ".$ipaddress);
130 fclose($f);
131 //MAKE SURE PERMISSIONS ARE SET 0644 to user...........
132
133#Connect to database
134 #On failed login insert data $ipaddress & $username
135 #Select relevant table and columns to monitor for patterns
136 #Set mysqli_num_rows to $ipfromlogs
137 if ($ipfromlogs > 3) {
138 // do your thing here with the insert of the above .htaccess code and perhaps a mail() or error_log() and perhaps a header() so people know they must contact you? Your choice.
139 }
140 else {
141 //Your code here for normal operation
142 }