1
0

session-based.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2012 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * simple session based user auth
  15. * There is no security check against enything.
  16. * Use this only as an example and not productive
  17. */
  18. # session
  19. define('SESSION_LIFETIME',28800); # default is 28800 => 8 hours
  20. define('SESSION_NAME','TheSessionName');
  21. define('AUTH_USER','the user name');
  22. define('AUTH_PASS','the password');
  23. define('AUTH_KEY','the special key');
  24. session_set_cookie_params(SESSION_LIFETIME);
  25. session_name(SESSION_NAME);
  26. session_start();
  27. session_regenerate_id(true);
  28. $needsLogin = true;
  29. if(isset($_GET['do']) && $_GET['do'] == "logout") {
  30. # clear session info
  31. session_destroy();
  32. $_COOKIE = array();
  33. $_SESSION = array();
  34. # "reload" the page
  35. header("Location: ./session-based.php"); # rename to the correct file!
  36. }
  37. elseif(isset($_SESSION[SESSION_NAME]['someKey']) && $_SESSION[SESSION_NAME]['someKey'] === AUTH_KEY) {
  38. $needsLogin = false;
  39. }
  40. # process the login form
  41. if(isset($_POST['doLogIn'])) {
  42. if(isset($_POST['username']) && isset($_POST['password'])) {
  43. $username = trim($_POST['username']);
  44. $password = trim($_POST['password']);
  45. if(!empty($username) && $username === AUTH_USER
  46. && !empty($password) && $password === AUTH_PASS) {
  47. # register the session
  48. $_SESSION[SESSION_NAME]['someKey'] = AUTH_KEY;
  49. $needsLogin = false;
  50. # "reload" the page
  51. header('Location: session-based.php'); # rename to the correct file!
  52. }
  53. }
  54. }
  55. header('Content-type: text/html; charset=UTF-8');
  56. ?>
  57. <html>
  58. <head>
  59. <title>SESSION based user auth</title>
  60. <meta charset='utf-8' />
  61. </head>
  62. <body>
  63. <h1>Simple $_SESSION based auth method</h1>
  64. <?php if($needsLogin === true) { ?>
  65. <h2>Login form</h2>
  66. <form method="post" action="">
  67. <label>Username</label>
  68. <input type="text" name="username" value="" />
  69. <br />
  70. <br />
  71. <label>Password</label>
  72. <input type="password" name="password" value="" /><br />
  73. <br />
  74. <button type="submit" name="doLogIn" title="Login">LogIn</button>
  75. </form>
  76. <?php } else { ?>
  77. <p>You are logged in.</p>
  78. <p><a href='?do=logout'>Do you want to logout ?</a></p>
  79. <?php } ?>
  80. </body>
  81. </html>