You can use the window.prompt(message) function.
password = window.prompt("Please enter your password.");
I'm not at home at the moment, I'll write you an example when I get back - if Mike or John hasn't got there first.
Printable View
You can use the window.prompt(message) function.
password = window.prompt("Please enter your password.");
I'm not at home at the moment, I'll write you an example when I get back - if Mike or John hasn't got there first.
passcheck.php:HTML Code:<script type="text/javascript">
function getPass(url) {
window.location.href = url + "?pass=" + urlencode(window.prompt("Please enter the password:"));
return false;
}
</script>
<a href="passcheck.php" onclick="getPass(this.href);">Go to password page</a>
Enjoy.PHP Code:<?php
$pass = $_GET['pass'];
if($pass == "john") {
// Do stuff
} else {
?>
<form method="get" action="<?=$PHP_SELF?>">
<input type="text" name="pass"/><input type="submit" value="OK"/>
</form>
<?php } ?>
So if I understand right, I make the passcheck.php its own page, in its own folder, then I make the link to that and it will show up as a pop up box?
Will this also protect the file I redirect to if you already know the URL?
And I am guessing i need to addAs well as change the GET_ to include the username...Code:<input type= "text" name= "user"/>
the problem I have now is that if you KNOW the exact URL to the page I want to protect you can bypass this pop up box. :confused:
I don't mind using the .htaccess, more what I want is to "make" my own, personalized, pop up box.
will this code do that?
Sort of.
For a username as well, you'll need two popup prompts because of the limitations of window.prompt().
It's never a good idea to redirect the user to another page when checking authentication. Try to include the check and the content on the same page.HTML Code:<script type="text/javascript">
function getPass(url) {
window.location.href = url
+ "?user=" + urlencode(window.prompt("Please enter your username:"))
+ "&pass=" + urlencode(window.prompt("Please enter the password:"));
return false;
}
</script>
<a href="passcheck.php" onclick="getPass(this.href);">Go to password page</a>
passcheck.php:
PHP Code:<?php
$user = $_GET['user'];
$pass = $_GET['pass'];
if($user == "john" && $pass == "travolta") {
?>
<!-- Success page goes here. -->
<html>
<head>
<title>Passcheck passed!</title>
</head>
<body>
<p>
Congratulations. You've passed the password check!
</p>
</body>
</html>
<?php
} else {
?>
<!-- Failure page goes here. We include an HTML form for non-JS users who couldn't use the prompts. -->
<html>
<head>
<title>Wrong Password</title>
</head>
<body>
<p>
Either you have entered the wrong username/password, or your browser does not support Javascript. Try again below.
</p>
<form method="get" action="<?=$PHP_SELF?>">
User: <input type="text" name="user"/><br/>
Password: <input type="text" name="pass"/>
<input type="submit" value="OK"/>
</form>
</body>
</html>
<?php } ?>