SilentQ-noob- Posted July 30, 2007 Share Posted July 30, 2007 I want to password protect a portfolio page on my website, but its not working out. I want to set a session variable to 0, then redirect to login if it does = 0. On the login page, once the password is entered, I set the variable to 1 and redirect back to portfolio- but it doesnt work. When I run the script with this code: portfolio.php <?php session_start(); $_SESSION['password'] = 0; if($_SESSION['password'] = 0) { header("location: login.php"); } ?> and this code on login.php <?php session_start(); if(isset($_POST['password'])) { if ($_POST['password'] == "example") { $_SESSION['password'] = 1; echo "Login Successful!" ; header("location: portfolio.php"); exit; } } ?> When I click on "portfolio" in the menu bar, it goes right to portfolio.php, and not to login Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 30, 2007 Share Posted July 30, 2007 Well, firstly you're using the assignment operator inside your if statement (single equals sign), which is always going to evaluate to true. However, you're also setting the value of $_SESSION['password'] to 0 right before you check it, so its never going to work. This should do the trick: <?php session_start(); if($_SESSION['password'] == 0) { header("location: login.php"); } ?> Quote Link to comment Share on other sites More sharing options...
SilentQ-noob- Posted July 30, 2007 Author Share Posted July 30, 2007 Thank you very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.