rotten69 Posted May 12, 2011 Share Posted May 12, 2011 Hey All, I'm tryin to make a log-in system for multiple usernames and passwords, but I don't really know how many if statements i'd need for it.. I'm also a noob.. <?php session_start(); $users = array("user1" =>"3202", "user2" =>"2002", "user3" =>"1061", "user4"=>"1400", "user5"=>"1001"); if($_REQUEST['username'] == "infs" && $_REQUEST['password'] == "3202"){ $_SESSION['username'] = "user1" ; $_SESSION['password'] = "3202" ; $_SESSION['username'] = "user2" ; $_SESSION['password'] = "2002" ; $_SESSION['username'] = "user5" ; $_SESSION['password'] = "1001" ; $_SESSION['username'] = "user3" ; $_SESSION['password'] = "1061" ; $_SESSION['username'] = "user4" ; $_SESSION['password'] = "1400" ; header("Location: home.php "); }else{ After checking if the matching username and password exist in my array then save them in a session... What's the best way of doing it? Quote Link to comment https://forums.phpfreaks.com/topic/236173-how-to-construct-a-log-in-system-with-multiple-usernames-and-passwords/ Share on other sites More sharing options...
gizmola Posted May 12, 2011 Share Posted May 12, 2011 You are definitely way off. Why don't you start by explaining what your current code is doing, and why you are checking $_REQUEST['username'] == "infs"? If this is reached via a POST then don't use $_REQUEST. Quote Link to comment https://forums.phpfreaks.com/topic/236173-how-to-construct-a-log-in-system-with-multiple-usernames-and-passwords/#findComment-1214260 Share on other sites More sharing options...
samato Posted May 12, 2011 Share Posted May 12, 2011 If you want to have multiple usernames and passwords, you need to look into databases, usually MySQL. Without this you the login system would be greatly flawed. Quote Link to comment https://forums.phpfreaks.com/topic/236173-how-to-construct-a-log-in-system-with-multiple-usernames-and-passwords/#findComment-1214296 Share on other sites More sharing options...
rotten69 Posted May 12, 2011 Author Share Posted May 12, 2011 I'm just trying to check if the username and password are matched with the ones in the array then a new session is on. if($_REQUEST['username'] == "user1" && $_REQUEST['password'] == "3202") then take them to the home page else stay on the same page.. so, having multiple usernames and passwords, how would you check for that? Would you do an if statement for each username/password? Quote Link to comment https://forums.phpfreaks.com/topic/236173-how-to-construct-a-log-in-system-with-multiple-usernames-and-passwords/#findComment-1214307 Share on other sites More sharing options...
gizmola Posted May 12, 2011 Share Posted May 12, 2011 Again, don't use $_REQUEST, use $_POST. Here's some code that should help you understand how to approach this: session_start(); $users = array("user1" =>"3202", "user2" =>"2002", "user3" =>"1061", "user4"=>"1400", "user5"=>"1001"); if (isset($_SESSION['username'])) { // already logged in. header('Location: home.php'); exit(); } elseif (isset($_POST['username']) && isset($_POST['password']) && array_key_exists($_POST['username'], $users) && $users[$_POST['username']] == $_POST['password']) { $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $users[$_SESSION['username']]; header('Location: home.php'); exit(); } else { // header() back to login form exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/236173-how-to-construct-a-log-in-system-with-multiple-usernames-and-passwords/#findComment-1214332 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.