Log in

View Full Version : protecting files



benslayton
06-01-2006, 01:25 PM
i need a password protection script that actually works, rather than the javascript stuff that can be figured out. They have these in PHP right...

Twey
06-01-2006, 01:52 PM
security.inc.php. Include at the very top of your page(s) with:
<?php require("security.inc.php"); ?>

<?php session_start();
$password = "your password goes here";

function showLoginForm($badpw = false) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Login Form</title>
</head>
<body>
<form action="<?php echo($PHP_SELF); ?>" method="post">
<p>
<?php if($badpw) { ?>
The password you entered was incorrect. Please try again.
<?php } ?>
<input type="password" name="pass">
<input type="submit" value="Log In">
</p>
</form>
</body>
</html>
<?php
return "";
}

if(!isset($_SESSION['pwhash']) && !isset($_POST['pass']))
die(showLoginForm());
else if(isset($_POST['pass']))
if($_POST['pass'] == $password) $_SESSION['pwhash'] = md5($password);
else die(showLoginForm(true));
else if(isset($_SESSION['pwhash']))
if($_SESSION['pwhash'] != md5($password)) die(showLoginForm(true));
?>

benslayton
06-01-2006, 04:56 PM
Thanks twey, that helps alot:) :)