Ricky55 Posted October 8, 2013 Share Posted October 8, 2013 Hi, I have the code below that using for a simple login system (security not a concern at all) This code works but it now has around ten if statements (not included them all for clarity), would this be best written as a switch statement? if so can you help me code it. I can write basic switch statements but I don't know how to match two cases. The login and the password. If a switch statement isn't the best option can you suggest what is and perhaps give me an example? Thanks guys! The code: <?php session_start(); $is_ajax = $_REQUEST['is_ajax']; if(isset($is_ajax) && $is_ajax) { $username = $_REQUEST['username']; $password = $_REQUEST['password']; if($username == 'richard' && $password == '123') { $_SESSION['isLoggedIn'] = true; echo ("login-richard"); } if($username == 'john' && $password == '456') { $_SESSION['isLoggedIn'] = true; echo ("login-john"); } } ?> Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 8, 2013 Solution Share Posted October 8, 2013 (edited) Rather than have a switch/case statement. You can just put all usernames/passwords into an array. like $users = array( 'username1' => 'password1', 'username2' => 'password2', // etc... ); Then use the following to proccess the login $is_ajax = $_REQUEST['is_ajax']; if(isset($is_ajax) && $is_ajax) { $username = $_REQUEST['username']; $password = $_REQUEST['password']; // check that the user exists in the $users array // and the password for that user matches the entered password if(isset($users[$username]) && $users[$username] == $password) { $_SESSION['isLoggedIn'] = true; echo ("login-$username"); } } However if you find you're adding more username/password entries then you may want to start looking into using a database. Edited October 8, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Ricky55 Posted October 8, 2013 Author Share Posted October 8, 2013 Thanks mate, really appreciate this! Quote Link to comment Share on other sites More sharing options...
Ricky55 Posted October 8, 2013 Author Share Posted October 8, 2013 Worked a treat btw. 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.