scriptygoddess

14 May, 2003

Password protect your page

Posted by: Jennifer In: Scripts

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

3 Responses to "Password protect your page"

1 | Megalion

May 15th, 2003 at 1:45 am

Avatar

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

2 | nicole

August 10th, 2004 at 6:37 am

Avatar

exactly wad can i identify on my template as a/the BODY tag? im terribly unsure where 2paste all d html stuffies above! sobz

3 | Erik van Roekel

August 27th, 2004 at 7:38 am

Avatar

Any idea how to password protect pages which uses the userid/password for authors of Movable type?

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements