Jump to content

[SOLVED] How to ban using htaccess?


L

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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... :-\

Link to comment
Share on other sites

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";

?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.