Forum Moderators: phranque
I was thinking I could set a cookie on successful login, and have each and every internal page of the site include a script that checks for a valid cookie, but I'm not sure this is the best way to do it. That also wouldn't block access to image files since no script is run when accessing an image file.
This is probably old hat to many of you but it's brand-new to me. Thank you very much for your help.
Here is a PHP sample for you:
All secured pages should include authorization file at the beginning:
include_once("authorize.php"); The athorization file (authorize.php) will have code like this:
require_once('Connections/cnDb.php');function authenticate() {
header("WWW-Authenticate: Basic realm=\"Your site… \"");
header("HTTP/1.0 401 Unauthorized");
print("You must enter a valid login username and password
to access this resource.\n");
exit;
}
mysql_select_db($database_ cnDb, $ cnDb);
if(!isset($PHP_AUTH_USER)){ authenticate(); }
else {
$q=sprintf("SELECT username, password FROM users
WHERE username='%s' AND password=PASSWORD('%s')",
$PHP_AUTH_USER,$PHP_AUTH_PW,$REMOTE_ADDR);
$q=mysql_query($q);
if(mysql_num_rows($q)==0){ authenticate(); }
}
I know if I used basic htpasswd authentication then that covers everything in a directory without the need to put a script on each page or specifically deny access to images, but is it appropriate to write to the htaccess/htpasswd files directly with a Perl script? I've always been wary of writing directly to any file that starts with a dot with a script....
Thanks for your help, -MBJ-