L Posted December 3, 2007 Share Posted December 3, 2007 Hey, Basically I have a table set up where if a user is banned his information(ip, username, etc...) is entered into a `banned` table. But now my question is, Is there a way to make it so each new ip address in the ban table is somehow used with htaccess to make sure that person can't access any files on the site? Also can this be done automatically, or will I have to enter each ip each time. Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2007 Share Posted December 3, 2007 you cannot read from a database (or any other data store) from within a .htaccess file. You will need to append your banned ips to the file each time, and yes, this can be done automatically. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted December 3, 2007 Share Posted December 3, 2007 As you know the .htaccess file has to look something like this: # Access restriction order allow,deny allow from all # Username deny from aaa.aaa.aaa.aaa # Another_username deny from bbb.bbb.bbb.bbb I'm not entirely sure if this is a good way to handle things, but you could simply update your .htaccess file each time you ban or unban a person. The possible drawback of this is that at some point the .htaccess file is being written to and saved at which point it might not work. So lets say a banned user tries to access some files while you are banning another user he might succeed... I don't know. To make this as simply as possible you could create your .htaccess file from scratch using entries from your database each time you ban or unban a person. That way you don't have to worry about how to add and remove users from the file... You simply create you file with all users in the banned table: <?php //Start of the .htaccess file $content = "# Access restriction\n order allow,deny\n allow from all\n\n"; //Get the banned users from the db $get_banned_users = mysql_query("SELECT username,ip FROM banned_users"); //Loop through all the banned users and create a deny from line for each while($banned_user = mysql_fetch_array($get_banned_users)) { $content .= " # " . $banned_user['username'] . "\n deny from " . $banned_user['ip'] . "\n\n"; } //Open the file for writing $handle = fopen("path/to/file/.htaccess", w); //Write the newly created list of banned users to the file fwrite($handle, $content); //Close the file fclose($handle); ?> The above code (a refined version of it suiting your needs) could be called each time you add or remove a user from banned_users table. It will create a .htaccess file like the one I showed above.. Hope this helps. Quote Link to comment Share on other sites More sharing options...
L Posted December 3, 2007 Author Share Posted December 3, 2007 Is there a way to make more than one htaccess file because my htaccess file already has some code I have in it, and I don't want that to get erased. EDIT: Or should I just have the htaccess file in another folder... :-\ Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted December 3, 2007 Share Posted December 3, 2007 If you go for an approach like the one I described (I'm still not sure if it's smart) you could simply place all your other .htaccess stuff in $content as well: <?php //Start of the .htaccess file $content = "# URL rewriting\n RewriteEngine on\n RewriteBase /basedir/\n RewriteRule ^([a-z]+)$ index.php?page=$1\n\n"; $content .= "# Access restriction\n order allow,deny\n allow from all\n\n"; ?> Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2007 Share Posted December 3, 2007 If your on a linux server you could also use sed to get it done in one easy command. <?php $ban = '192.168.10.1'; exec("sed -i '/allow from all/ a\deny from $ban' .htaccess"); ?> Quote Link to comment Share on other sites More sharing options...
L Posted December 4, 2007 Author Share Posted December 4, 2007 It's giving me a warning about how exec is disabled for security reasons. how can I fix this? Quote Link to comment Share on other sites More sharing options...
trq Posted December 5, 2007 Share Posted December 5, 2007 You can't. Shared hosts will do that sometimes. Quote Link to comment Share on other sites More sharing options...
L Posted December 5, 2007 Author Share Posted December 5, 2007 ugh, alright then... but for the first way I get these errors when I try using it. Warning: fopen(path/to/file/../iv/ht/.htaccess) [function.fopen]: failed to open stream: No such file or directory in /home2/iffatb/public_html/iv/forums/ban.php on line 65 Warning: fwrite(): supplied argument is not a valid stream resource in /home2/iffatb/public_html/iv/forums/ban.php on line 68 Warning: fclose(): supplied argument is not a valid stream resource in /home2/iffatb/public_html/iv/forums/ban.php on line 71 line 65: $handle = fopen("path/to/file/../iv/ht/.htaccess", w); line 68 and 71: fwrite($handle, $content); fclose($handle); the file is located at forums.site.net htaccess file is located at site.net Quote Link to comment Share on other sites More sharing options...
trq Posted December 6, 2007 Share Posted December 6, 2007 The example path.... "path/to/file/.htaccess" is intended to be replaced with yours, not to be used literally! Try... $handle = fopen("/home2/iffatb/public_html/.htaccess", w); Quote Link to comment Share on other sites More sharing options...
L Posted December 6, 2007 Author Share Posted December 6, 2007 OMG!!! That just solved this problem and my uploading image problem...how stupid can I be :( I feel so ashamed!!!!!! Thanks a lot...(*shuns self*) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.