Portal Home > Knowledgebase > Articles Database > php, writing to .htaccess ?


php, writing to .htaccess ?




Posted by acctman, 06-13-2009, 02:41 PM
hi can someone assist me with help on how to write to a .htaccess file and insert a deny IP. what i'll need to do is insert above and /n for new line

Posted by Kohrar, 06-14-2009, 06:41 AM
Interesting problem. If you want to go all crazy with fseek, here is my solution: Just make sure that your .htaccess has a new line after for it to work properly. Otherwise, I'd use file and put it before the last line, but you're opening the file twice and rewriting the entire file with this approach: I think the second one may be a little bit more robust, but they both should work. Last edited by Kohrar; 06-14-2009 at 06:46 AM.

Posted by Kohrar, 06-14-2009, 05:16 PM
Sorry about double posting, but I thought of another solution based on the second one when I went to bed which will be less problematic than the two I gave above: This will be a bit ineffecient, but since it is a .htaccess file, effeciency doesn't really matter too much. This will write your 'deny from x.x.x.x' right before the line. So there are no magic numbers to deal with unlike the implementation I gave in the previous post.

Posted by foobic, 06-14-2009, 05:36 PM
I'd suggest a different method altogether. Instead of editing .htaccess every time, have your PHP script create a file named after each IP address you want to ban, then use mod_rewrite in .htaccess to detect the file and deny access.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Problem with ld.so (Views: 610)
Resellers, who's good? (Views: 579)