Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. <?
  2. include('config.php');
  3.  
  4. function confirmUser($username, $password){
  5. global $conn;
  6. /* Add slashes if necessary (for query) */
  7. if(!get_magic_quotes_gpc()) {
  8. $username = addslashes($username);
  9. }
  10.  
  11. /* Verify that user is in database */
  12. $q = "select password from users where username = '$username'";
  13. $result = mysql_query($q,$conn);
  14. if(!$result || (mysql_numrows($result) < 1)){
  15. return 1; //Indicates username failure
  16. }
  17.  
  18. /* Retrieve password from result, strip slashes */
  19. $dbarray = mysql_fetch_array($result);
  20. $dbarray['password'] = stripslashes($dbarray['password']);
  21. $password = stripslashes($password);
  22.  
  23. /* Validate that password is correct */
  24. if($password == $dbarray['password']){
  25. return 0; //Success! Username and password confirmed
  26. }
  27. else{
  28. return 2; //Indicates password failure
  29. }
  30. }
  31.  
  32. /**
  33. * checkLogin - Checks if the user has already previously
  34. * logged in, and a session with the user has already been
  35. * established. Also checks to see if user has been remembered.
  36. * If so, the database is queried to make sure of the user's
  37. * authenticity. Returns true if the user has logged in.
  38. */
  39. function checkLogin(){
  40. /* Check if user has been remembered */
  41. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
  42. $_SESSION['username'] = $_COOKIE['cookname'];
  43. $_SESSION['password'] = $_COOKIE['cookpass'];
  44. }
  45.  
  46. /* Username and password have been set */
  47. if(isset($_SESSION['username']) && isset($_SESSION['password'])){
  48. /* Confirm that username and password are valid */
  49. if(confirmUser($_SESSION['username'], $_SESSION['password']) != 0){
  50. /* Variables are incorrect, user not logged in */
  51. unset($_SESSION['username']);
  52. unset($_SESSION['password']);
  53. return false;
  54. }
  55. return true;
  56. }
  57. /* User not logged in */
  58. else{
  59. return false;
  60. }
  61. }
  62.  
  63. /**
  64. * Determines whether or not to display the login
  65. * form or to show the user that he is logged in
  66. * based on if the session variables are set.
  67. */
  68. function displayLogin(){
  69. global $logged_in;
  70. if($logged_in){
  71. echo "Hello, <b>$_SESSION[username]</b>, <a href=\"logout.php\">Logout</a>";
  72. }
  73. else{
  74. echo "Test (Don't worry if you see this)"
  75. ?>
  76.  
  77. <h1>Login</h1>
  78. <form action="" method="post">
  79. <table align="left" border="0" cellspacing="0" cellpadding="3">
  80. <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
  81. <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
  82. <tr><td colspan="2" align="left"><input type="checkbox" name="remember">
  83. <font size="2">Remember me next time</td></tr>
  84. <tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
  85. <tr><td colspan="2" align="left"><a href="register.php">Join</a></td></tr>
  86. </table>
  87. </form>
  88.  
  89. <?
  90. }
  91. }
  92.  
  93.  
  94. /**
  95. * Checks to see if the user has submitted his
  96. * username and password through the login form,
  97. * if so, checks authenticity in database and
  98. * creates session.
  99. */
  100. if(isset($_POST['sublogin'])){
  101. /* Check that all fields were typed in */
  102. if(!$_POST['user'] || !$_POST['pass']){
  103. die('You didn\'t fill in a required field.');
  104. }
  105. /* Spruce up username, check length */
  106. $_POST['user'] = trim($_POST['user']);
  107. if(strlen($_POST['user']) > 30){
  108. die("Sorry, the username is longer than 30 characters, please shorten it.");
  109. }
  110.  
  111. /* Checks that username is in database and password is correct */
  112. $md5pass = md5($_POST['pass']);
  113. $result = confirmUser($_POST['user'], $md5pass);
  114.  
  115. /* Check error codes */
  116. if($result == 1){
  117. die('That username doesn\'t exist in our database.');
  118. }
  119. else if($result == 2){
  120. die('Incorrect password, please try again.');
  121. }
  122.  
  123. /* Username and password correct, register session variables */
  124. $_POST['user'] = stripslashes($_POST['user']);
  125. $_SESSION['username'] = $_POST['user'];
  126. $_SESSION['password'] = $md5pass;
  127.  
  128. /**
  129. * This is the cool part: the user has requested that we remember that
  130. * he's logged in, so we set two cookies. One to hold his username,
  131. * and one to hold his md5 encrypted password. We set them both to
  132. * expire in 100 days. Now, next time he comes to our site, we will
  133. * log him in automatically.
  134. */
  135. if(isset($_POST['remember'])){
  136. setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
  137. setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
  138. }
  139.  
  140. /* Quick self-redirect to avoid resending data on refresh */
  141. echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
  142. return;
  143. }
  144.  
  145. /* Sets the value of the logged_in variable, which can be used in your code */
  146. $logged_in = checkLogin();
  147.  
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement