You can, but it's not a good idea. Javascript password protection is very insecure, and when you have PHP available anyway, it's better to use that.
PHP Code:
<?php
# security.inc.php (in the same directory as your pages)
$mypass = "yourPassGoesHere";
$prompt = <<<END
<html>
<head>
<title>Please enter password</title>
</head>
<body>
<form action="
END;
$prompt .= $_SERVER['HTTP_REFERER'];
$prompt .= <<<END
" method="post">
<input type="password" name="password"/>
<input type="submit" value="OK"/>
</form>
</body>
</html>
END;
session_start();
if(isset($_POST['password'])) {
if($_POST['password'] != $mypass) {
die(
$prompt);
} else {
session_register("password");
if($_POST['password'] == $mypass) $_SESSION['password'] = $_POST['password'];
}
} else if(!$_SESSION['password']) {
die($prompt);
}
?>
Edit as needed.
Then include it at the top of your protected .php page like so:
<?php include("security.inc.php"); ?>
Note that this way, when the user enters the password once, s/he won't have to enter it again to use any page that includes security.inc.php if s/he has cookies enabled. If you want another login for other pages, you must create another include page, and change every instance of "password" to something else.
Bookmarks