Advertisement
Guest User

Untitled

a guest
May 20th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. <?
  2. function confirmUser($username, $password){
  3. global $conn;
  4. if(!get_magic_quotes_gpc())
  5. $username = addslashes($username);
  6. $q = "select password from users where username = '$username'";
  7. $result = mysql_query($q,$conn);
  8. if(!$result || (mysql_numrows($result) < 1)){
  9. return 1; //Indicates username failure
  10. }
  11. $dbarray = mysql_fetch_array($result);
  12. $dbarray['password'] = stripslashes($dbarray['password']);
  13. $password = stripslashes($password);
  14. if($password == $dbarray['password']){
  15. return 0; //Success! Username and password confirmed
  16. }
  17. else{
  18. return 2; //Indicates password failure
  19. }
  20. }
  21.  
  22.  
  23. function checkLogin(){
  24.  
  25. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
  26. $_SESSION['username'] = $_COOKIE['cookname'];
  27. $_SESSION['password'] = $_COOKIE['cookpass'];
  28. }
  29.  
  30.  
  31. if(isset($_SESSION['username']) && isset($_SESSION['password'])){
  32.  
  33. if(confirmUser($_SESSION['username'], $_SESSION['password']) != 0){
  34.  
  35. unset($_SESSION['username']);
  36. unset($_SESSION['password']);
  37. return false;
  38. }
  39. return true;
  40. }
  41.  
  42. else{
  43. return false;
  44. }
  45. }
  46.  
  47.  
  48. function displayLogin(){
  49. global $logged_in;
  50. if($logged_in){
  51. echo "<h1>Logged In!</h1>";
  52. echo "Welcome <b>$_SESSION[username]</b>, you are logged in. <a href=\"logout.php\">Logout</a>";
  53. }
  54. else{
  55. ?>
  56.  
  57. <h1>Login</h1>
  58. <form action="" method="post">
  59. <table align="left" border="0" cellspacing="0" cellpadding="3">
  60. <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
  61. <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
  62. <tr><td colspan="2" align="left"><input type="checkbox" name="remember">
  63. <font size="2">Remember me next time</td></tr>
  64. <tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
  65. <tr><td colspan="2" align="left"><a href="register.php">Join</a></td></tr>
  66. </table>
  67. </form>
  68.  
  69. }
  70.  
  71. <?
  72. }
  73.  
  74.  
  75.  
  76. if(isset($_POST['sublogin'])){
  77.  
  78. if(!$_POST['user'] || !$_POST['pass']){
  79. die('You did not fill in a required field.');
  80. }
  81.  
  82. $_POST['user'] = trim($_POST['user']);
  83. if(strlen($_POST['user']) > 30){
  84. die("Sorry, the username is longer than 30 characters, please shorten it.");
  85. }
  86.  
  87.  
  88. $md5pass = md5($_POST['pass']);
  89. $result = confirmUser($_POST['user'], $md5pass);
  90.  
  91.  
  92. if($result == 1){
  93. die('That username does not exist in our database.');
  94. }
  95. else if($result == 2){
  96. die('Incorrect password, please try again.');
  97. }
  98.  
  99.  
  100. $_POST['user'] = stripslashes($_POST['user']);
  101. $_SESSION['username'] = $_POST['user'];
  102. $_SESSION['password'] = $md5pass;
  103.  
  104.  
  105. if(isset($_POST['remember'])){
  106. setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
  107. setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
  108. }
  109.  
  110.  
  111. echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
  112. return;
  113. }
  114.  
  115.  
  116. $logged_in = checkLogin();
  117.  
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement