Advertisement
Guest User

login

a guest
Jan 12th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. <?php
  2.  
  3. //Connects to your Database
  4. $conect = mysqli_connect("localhost:3307","root","usbw", "auth") or die(mysql_error());
  5.  
  6. //Checks if there is a login cookie
  7. if(isset($_COOKIE['ID_your_site'])){ //if there is, it logs you in and directes you to the members page
  8. $username = $_COOKIE['ID_your_site'];
  9. $pass = $_COOKIE['Key_your_site'];
  10. $check = mysqli_query($conect, "SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
  11.  
  12. while($info = mysqli_fetch_array( $check )){
  13. if ($pass != $info['password']){}
  14. else{
  15. header("Location: login.php");
  16. }
  17. }
  18. }
  19.  
  20. //if the login form is submitted
  21. if (isset($_POST['submit'])) {
  22.  
  23. // makes sure they filled it in
  24. if(!$_POST['username']){
  25. die('You did not fill in a username.');
  26. }
  27. if(!$_POST['pass']){
  28. die('You did not fill in a password.');
  29. }
  30.  
  31. // checks it against the database
  32. if (!get_magic_quotes_gpc()){
  33. $_POST['email'] = addslashes($_POST['email']);
  34. }
  35.  
  36. $check = mysqli_query($conect, "SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
  37.  
  38. //Gives error if user dosen't exist
  39. $check2 = mysqli_num_rows($check);
  40. if ($check2 == 0){
  41. die('That user does not exist in our database.<br /><br />If you think this is wrong <a href="login.php">try again</a>.');
  42. }
  43.  
  44. while($info = mysqli_fetch_array( $check )){
  45. $_POST['pass'] = stripslashes($_POST['pass']);
  46. $info['password'] = stripslashes($info['password']);
  47. $_POST['pass'] = md5($_POST['pass']);
  48.  
  49. //gives error if the password is wrong
  50. if ($_POST['pass'] != $info['password']){
  51. die('Incorrect password, please <a href="login.php">try again</a>.');
  52. }
  53.  
  54. else{ // if login is ok then we add a cookie
  55. $_POST['username'] = stripslashes($_POST['username']);
  56. $hour = time() + 3600;
  57. setcookie(ID_your_site, $_POST['username'], $hour);
  58. setcookie(Key_your_site, $_POST['pass'], $hour);
  59.  
  60. //then redirect them to the members area
  61. header("Location: members.php");
  62. }
  63. }
  64. }
  65. else{
  66. // if they are not logged in
  67. ?>
  68.  
  69. <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
  70.  
  71. <table border="0">
  72.  
  73. <tr><td colspan=2><h1>Login</h1></td></tr>
  74.  
  75. <tr><td>Username:</td><td>
  76.  
  77. <input type="text" name="username" maxlength="40">
  78.  
  79. </td></tr>
  80.  
  81. <tr><td>Password:</td><td>
  82.  
  83. <input type="password" name="pass" maxlength="50">
  84.  
  85. </td></tr>
  86.  
  87. <tr><td colspan="2" align="right">
  88.  
  89. <input type="submit" name="submit" value="Login">
  90.  
  91. </td></tr>
  92.  
  93. </table>
  94.  
  95. </form>
  96.  
  97. <?php
  98. }
  99. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement