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