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.";
}
?> |









Oh thanks. I could use that ;) Is it… safe? :P
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 ^_^