Hi,
You could do a couple of things.
First, you could use an .htaccess file to limit access to the entire directory.
Create a .txt file and insert the following code:
Code:
AuthName "My Website"
AuthType Basic
AuthUserFile "C:\xampp\safedirectory\mysite.users"
require valid-user
<Files .htaccess>
order allow,deny
deny from all
</Files>
Name this file .htaccess and place it in the directory you want to protect.
Next, create another .txt file, I called this one "mysite.users" and type in the following:
Place the "mysite.users" file in the "safedirectory" referenced in the .htaccess file you created in the first step.
The safe directory should be one level above your public web root. So in your case you could put the "mysite.users" file in a folder above your localhost root. Maybe in the same directory that holds "htdocs" if you are using Apache.
Now when you attempt to access any file in the directory that holds .htaccess, you will be prompted to supply a username and password. Use the username and password combination that you put in your "mysite.users" file.
The second way would be to use php $_SESSION variables and some code to check and see if the person trying to access the pages in that directory has the authority to do so.
In this case, set something like:
Code:
$_SESSION['ADMIN_ACCESS'] = true;
when the authorized user logs in. Then in everypage inside your /admin/ directory (or any other page you want to restrict access to) you'll put:
Code:
<?php session_start();
if ( isset($_SESSION['ADMIN_ACCESS']) ) {
if ( $_SESSION['ADMIN_ACCESS'] === true ) {
Protected content here.
} else {
exit("You aren't allowed");
}
} else {
exit("You aren't allowed");
}
?>
I am very interested in this subject too, so if anyone else has any ideas, or ways to improve what I suggested, I would love to hear them.
Good Luck!
JasonDFR
Bookmarks