Hi, I have this simple script. I need to define more than 1 user for this and also assign name for the username. Any ideas ? <?php session_start(); $username = "user1"; $password = "pass1"; $name = "User"; if(isset($_GET['logout'])) { unset($_SESSION["login"]); echo "You have logged out ... "; echo "[<a href='index.php'>Login</a>]"; exit; } if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !isset($_SESSION["login"])) { header("WWW-Authenticate: Basic realm=\"Agents\""); header("HTTP/1.0 401 Unauthorized"); $_SESSION["login"] = true; echo "You are unauthorized ... "; echo "[<a href='index.php'>Login</a>]"; exit; } else { if($_SERVER['PHP_AUTH_USER'] == $username && $_SERVER['PHP_AUTH_PW'] == $password) { echo "Welcome $name"; } else { unset($_SESSION["login"]); header("Location:index.php"); } } ?> PHP:
Try this: <?php session_start(); //array of username => password $login_info = array('user1' => 'pass1'); if (isset($_GET['logout'])) { unset($_SESSION["login"]); echo "You have logged out ... "; echo "[<a href='index.php'>Login</a>]"; exit; } if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !isset($_SESSION["login"])) { header("WWW-Authenticate: Basic realm=\"Agents\""); header("HTTP/1.0 401 Unauthorized"); $_SESSION["login"] = true; echo "You are unauthorized ... "; echo "[<a href='index.php'>Login</a>]"; exit; } else { if (array_key_exists($_SERVER['PHP_AUTH_USER'], $login_info) && $login_info[$username] == $_SERVER['PHP_AUTH_PW']) { echo "Welcome {$_SERVER['PHP_AUTH_USER']}"; } else { unset($_SESSION["login"]); header("Location:index.php"); } } ?> PHP: You can edit $login_info to add more 'accounts'.
Thanks verymuch for the effort. But the code does not work. I mean it does not accept the username & pass. Tried a few different ways but still could not get it to work.. Any ideas ?
I just modified your code, theirfore it should'nt effect functionality, however I rewrote your code here you go: <?php session_start(); //array of username => password $login_info = array('admin' => 'pass', 'danx10' => 'dppass'); $file = basename(__FILE__); if (isset($_GET['logout'])) { unset($_SESSION["username"]); unset($_SESSION["login"]); header("Location: {$file}"); } if (!isset($_SESSION['login'])) { if (isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; $errors = array(); if (strlen($username) <= 1) { $errors[] = "The username is too short."; } if (strlen($password) <= 1) { $errors[] = "The password is too short."; } if (!array_key_exists($username, $login_info) || $login_info[$username] != $password) { $errors[] = "Username or password are incorrect"; } if (empty($errors)) { $_SESSION["username"] = $username; $_SESSION["login"] = true; header("Location: {$file}"); } else { $out = "<ul>"; foreach ($errors as $error) { $out .= "<li>{$error}</li>"; } $out .= "</ul>"; echo $out; } } else { ?> <form method="post"> Username: <input type="text" name="username"><br /> Password: <input type="password" name="password"><br /> <input type="submit" name="submit" value="Login"> </form> <?php } } else { ?> Hello <?php echo $_SESSION["username"]; ?>, you are logged in<br /> <a href="?logout">Logout?</a> <?php /* private content should go here */ } ?> PHP:
Thanks danx10, it worked just fine.. One extra question. If I was to add a detail for that user, ie. membership number or some detail to print on the secure content, how would I do it ? Something like " $login_info = array('admin' => 'pass' => '4223', 'danx10' => 'dppass' =>'4224'); "