- Joined
- Jan 16, 2011
- Messages
- 55
- Reaction score
- 2
How can I make some text appear only if the person outputted a certain (password) text.
thanks.
thanks.
You'll need to give more details on this one. Do you mean, if the user's input is incorrect, you make some text appear?
<?php
$password = "PASSWORD"; //Set the required password here.
if($_SERVER['REQUEST_METHOD'] == "POST") { //If the form was submitted,
if ($_POST['passcode'] != $password) { //Fail
echo '<b>WRONG PASSWORD!</b>';
} else { //Success
echo '<i>The secret to life is: <b>42</b>.</i>';
}
echo '<br><a href="#" onclick="history.go(-1);">Back</a>'; //Display back button below the fail/success text.
} else { //No password entered yet, let's display the form.
echo '<form name="myform" action="index.php" method="post">';
echo 'Password:';
echo '<br><input type="password" id="passcode" name="passcode">';
echo '<br><input type="submit" id="button" value="Go">';
echo '</form>';
}
?>
I just wrote this up for you...
PHP:<?php $password = "PASSWORD"; //Set the required password here. if($_SERVER['REQUEST_METHOD'] == "POST") { //If the form was submitted, if ($_POST['passcode'] != $password) { //Fail echo '<b>WRONG PASSWORD!</b>'; } else { //Success echo '<i>The secret to life is: <b>42</b>.</i>'; } echo '<br><a href="#" onclick="history.go(-1);">Back</a>'; //Display back button below the fail/success text. } else { //No password entered yet, let's display the form. echo '<form name="myform" action="index.php" method="post">'; echo 'Password:'; echo '<br><input type="password" id="passcode" name="passcode">'; echo '<br><input type="submit" id="button" value="Go">'; echo '</form>'; } ?>
The code works fine and is commented. Enjoy.
<?php
// Set up your password / other variables.
$password = "mypass";
$goback = "<a href='javascript:history.go(-1)'>go back</a>";
// Check if form was submitted.
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// Form was submitted, welcome the user.
echo "<p>Hello, " . $_POST['user'] . ".";
// Check if password matches $password.
if ($_POST['password'] == $password) {
// Password was correct, access granted.
echo "<p>Your password was correct.</p>";
echo "<p>Welcome to the system!";
echo "<p>$goback</p>";
} else {
// Password was incorrect, return to log-in page.
echo "<p>Your password was incorrect.</p>";
echo "<p>Please $goback and try again.";
}
} else {
// Form has not been submitted yet, display log-in form.
// Escape out of PHP to display the form for easier code readability.
?>
<p>Please enter your username and password.</p>
<form name="checkpass" action="./index.php" method="post">
<p>Username: <input type="text" name="user"/></p>
<p>Password: <input type="password" name="password"/>
<input type="submit" name="submit" value="Go"/></p>
</form>
<?php
// Return to PHP to close the IF statement, end of script.
}
?>