Password protect a page

Password protection for a single page. Visitors are required to enter a password and username into a login form to view the page content. This is not the most secure script around so use at care. It basically keeps the general visitor at bay.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
< ?php
//Define your username and password
$username = "someuser";
$password = "somepassword";
if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?>
<!-- Define the form -->
<h1>Login</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>;" method="post">
<label for="txtUsername">Username:</label>
<input title="Enter your Username" name="txtUsername" type="text" />
<label for="txtpassword">Password:</label>
<input title="Enter your password" name="txtPassword" type="password" />
<input name="Submit" type="submit" value="Login" />
</form>
< ?php
}
else {
//Define page output
echo "<p>This is the protected page. Your private content goes here.";
}
?>
Aug 5th, 2006 | Posted in PHP
Tags:
  1. Aug 11th, 2006 at 19:58 | #1

    Oh thanks. I could use that ;) Is it… safe? :P

  2. Aug 12th, 2006 at 08:59 | #2

    Well, that depends on how you look at it. Its not safe since its not using any cryptet algorytm, but if safe enough for mediocre stuff.
    To have it a little more secure would be to have the username and password crypted in mysql and call it from there, or to call it from a another page maybe.

    This script is more like something easy to use for the less experienced or to pwd protect something without the fancy 3 hours coding work to make it work ^_^

Leave a comment

XHTML: Usable tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Disclaimer: For any content that you post, you hereby grant to Deadhouse Gates the royalty-free, irrevocable, perpetual, exclusive and fully sublicensable license to use, reproduce, modify, adapt, publish, translate, create derivative works from, distribute, perform and display such content in whole or in part, world-wide and to incorporate it in other works, in any form, media or technology now known or later developed. Some rights reserved.