July 7, 200917 yr Hi guys I need a little help again. I am using this very simple php script to password protect content on a website. <?php $password = "frog";?> <?php // If password is valid let the user get access if (isset($_POST["password"]) && ($_POST["password"]=="$password")) { ?> <!-- START OF HIDDEN HTML --> <p> </p> <!-- END OF HIDDEN HTML --> <?php } else { // Wrong password or no password entered display this message if (isset($_POST['password']) || $password == "") { print "<h4>Incorrect Password</h4><h4>Please enter the correct password</h4>";} print "<form method=\"post\" action=\"\"><p>Please enter your password for access<br />"; print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\" /><input value=\"Login\" type=\"submit\" /></p></form>"; } ?> Live example here On one of the sites I am using this script, the owner now want it so that if you navigate away from the page, pressing the back button will require re-entering the password. In addition they would like a logout button. I know almost nothing about php and would really be most appreciative if someone could help me adapt this script to accommodate their needs. Many thanks Cara
July 7, 200917 yr In that case you will need to set up a session. I am definitely no PHP guru so I will point you to: http://www.w3schools.com/PHP/php_sessions.asp Which should cover most of what you need to do
July 7, 200917 yr Add this to the top of the page <?php session_start(); ?> Also you could add this: <?php if (!isset($_SESSION["auth_username"])) { echo "You must be logged in to view this page"; exit; } else { echo ""; } ?> the auth_username you can change to auth_password but use this line of code too: To use logout, just create a form with a button instead like so: <form action="logout.php" method="post"> <input type="submit" name="sent" id="sent" value="Logout" /></form> then in the logout.php file you just put the following code: <?php session_start(); session_destroy(); header("Location: success.php"); //This is the location where you want the user to go after the session has been destroyed ?> Hope this helps
July 7, 200917 yr Author Add this to the top of the page <?php session_start(); ?> Also you could add this: <?php if (!isset($_SESSION["auth_username"])) { echo "You must be logged in to view this page"; exit; } else { echo ""; } ?> the auth_username you can change to auth_password but use this line of code too: To use logout, just create a form with a button instead like so: <form action="logout.php" method="post"> <input type="submit" name="sent" id="sent" value="Logout" /></form> then in the logout.php file you just put the following code: <?php session_start(); session_destroy(); header("Location: success.php"); //This is the location where you want the user to go after the session has been destroyed ?> Hope this helps Hi lstables Thanks. I can't seem to get it to work as the back button still displays password protected content
July 7, 200917 yr Im a bit confused by: // Wrong password or no password entered display this message if (isset($_POST['password']) || $password == "") { If password isset or password == "" Incurrect login message.... everytime some one trys to login they will get that message
July 7, 200917 yr Author Im a bit confused by: // Wrong password or no password entered display this message if (isset($_POST['password']) || $password == "") { If password isset or password == "" Incurrect login message.... everytime some one trys to login they will get that message Have no idea how it works, just know that it does!
July 7, 200917 yr I'm not sure then, I still learning PHP and all the above works for me maybe you show us the full code ?!?! If not then am sure someone on here will help, there are a lot of knowledgable people on here...
July 7, 200917 yr Author I'm not sure then, I still learning PHP and all the above works for me maybe you show us the full code ?!?! If not then am sure someone on here will help, there are a lot of knowledgable people on here... I have given you the fill code. Thanks anyways, hopefully someone else will have the answer.
July 7, 200917 yr Im a bit confused by: // Wrong password or no password entered display this message if (isset($_POST['password']) || $password == "") { If password isset or password == "" Incurrect login message.... everytime some one trys to login they will get that message It works because it's in a different section of the "if" statement. "If login password is correct then do x" else "if login password is set or blank then do x". The second part of that statement is only active if the password is wrong. So it doesn't matter. It's poor coding but it works. It should be: // Wrong password or no password entered display this message if (!isset($_POST['password']) || $password == "") { If password is not set or password is blank. You could further cut it down to just: // Wrong password or no password entered display this message if (!isset($_POST['password'])) { I can post the answer later don't have time right now I need to go out.
Create an account or sign in to comment