Password protect your page
I wrote this script to password-protect a page of mine. This has some disadvantages in that you need to implement this on every page you want to password protect, though you can throw it into a header/footer file to cover a set of pages. I use this in MT to password protect a private journal of mine.
First, post this on the VERY FIRST LINE of the code… it has to come before any <HTML> or header tags… It MUST start on the very first line, or it won't work. Change the URLs to the page you want it to go to when you log in, and change USERNAME and PASSWORD to whatever you choose.
<?php
// Set a cookie that expires in one year
if($submitcookie){
if($loginname=="USERNAME" && $loginpassword=="PASSWORD"){
setcookie("login", $loginname, time()+31536000);
header("Location: http://YOURDOMAIN.COM/PATH/TO/PAGE/");
exit;
} else {
$incorrectlogin = true;
}
}
if($deletecookie){
setcookie("login", $loginname, time()-31536000);
header("Location: http://YOURDOMAIN.COM/PATH/TO/PAGE/");
}
?>
Next, paste this right after your BODY tag, before anything you want protected.
<?php
// if LOGGED IN
if (isset($login)){
?>
Somewhere in the page include this link to let you log out. This can be placed anywhere in your design. =)
<a href="<?php echo $PHP_SELF ?>?deletecookie=true">Log Out</a>
And lastly at the very end of the page, before the BODY tag and after everything you want protected, paste this chunk of code. You can format it so it looks nice and fits in with your design, as long as you don't change the mechanics (field names, etc).
<?
// if not logged in
} else {
?>
<table border=0 cellpadding=0 cellspacing=0 align=center>
<tr>
<td>
<?php if($incorrectlogin){ ?>
<div id="entry" align=center style="text-align: center;">
Username/password incorrect.
</div>
<?php } ?>
<form action="<?php echo $PHP_SELF; ?>" name="login">
<div style="font-weight: bold;">Please Log In</div>
<div align=center style="text-align: center; margin-top:8px;">
<table border=0 cellpadding=0 cellspacing=2>
<tr><td> User Name</td>
<td><input type=text size=20 name="loginname" class="forms"></td> </tr>
<tr><td>Password</td>
<td><input type=password size=20 name="loginpassword" class="forms"></td> </tr>
<tr><td colspan=2 align=center>
<input type=submit name="submitcookie" value="Login" class="forms2"> </td></tr>
</table>
</div>
</form>
</td></tr>
</table>
<?php
} // end if/else logged in
?>
So you're basically making a sandwich of your page contents. And that's it!
Guest Authored by:
Natalie - lunardreams.net
May 15th, 2003 at 1:45 am
Another way….
Here's a very easy to follow tutorial on how to use .htaccess to password protect pages/directories.
Only works with sites running on apache though.
http://www.javascriptkit.com/howto/htaccess3.shtml
August 10th, 2004 at 6:37 am
exactly wad can i identify on my template as a/the BODY tag? im terribly unsure where 2paste all d html stuffies above! sobz
August 27th, 2004 at 7:38 am
Any idea how to password protect pages which uses the userid/password for authors of Movable type?