Guest User

Untitled

a guest
Jun 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. $creds = array(
  2. array( 'username' => 'john123',
  3. 'password' => 'hello'),
  4. array( 'username' => 'lucyliu',
  5. 'password' => 'dieblackmamba')
  6. //more sub arrays like above here
  7. );
  8.  
  9. $username = $_POST['username'];
  10. $password = $_POST['password'];
  11.  
  12. $match = false;
  13. for($i=0;$i<count($creds);$i++) {
  14. if(strcmp($username,$creds[$i]['username']) == 0) {
  15. if(strcmp($password,$creds[$i]['password']) == 0) {
  16. // start session and set something to indicate that user is logged in
  17. session_start();
  18. $_SESSION['authenticated'] = true;
  19. $_SESSION['username'] = $creds[$i]['username'];
  20. $match = true;
  21. }
  22. }
  23. }
  24.  
  25. if($match) echo 'Login successful: welcome ' . $creds[$i]['username'];
  26. else echo 'Invalid credentials';
  27.  
  28. session_start();
  29. if($_SESSION['authenticated'] === true) {
  30. echo 'User ' . $_SESSION['username'] . ' is logged in.';
  31. }
  32.  
  33. $_SESSION = array();
  34. // If it's desired to kill the session, also delete the session cookie.
  35. // Note: This will destroy the session, and not just the session data!
  36. if (isset($_COOKIE[session_name()])) {
  37. setcookie(session_name(), '', time()-42000, '/');
  38. }
  39. // Finally, destroy the session.
  40. session_destroy();
Add Comment
Please, Sign In to add comment