PHP password redirect

Dan560 used Ask the Experts™

I want to password protect a page that prompts a user for a password. Depending on what the password is I want the user to be redirected to a page. for example

if the password = user1 redirect the user to www.mysite/user1
if the password = user2 redirect the user to www.mysite/user2

Can anyone help me?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can also try with .htaccess way. ref. Password protect
OR do you want it to do in pure php?


I do not mind, just a simple solution will do. The site will not be made to the public so it does not need to be secure.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

try something like this,

$password_1 = 'user1'; // Set your password here
$password_2 = 'user2'; // Set your password here

$pass = $_POST['pass']; // pass should be the name of the textfield.

if ($pass == $password_1) { 
   header("Location: www.mysite/user1/"); 
} elseif ($pass = $password_2)
    header("Location: www.mysite/user2/"); 
else {
      echo "Wrong password";

Open in new window

correct this line, } elseif ($pass == $password_2)

use ==
Most Valuable Expert 2011
Top Expert 2016
password protect a page that prompts a user for a password
That sounds kind of counterintuitive to me.  Usually the page that asks for a password is not protected -- it is there to protect other pages.  See the use of the access_control() function in this article for details.

HTH, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial