Advertisement
Guest User

Untitled

a guest
Dec 25th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. <?php
  2. function Connect()
  3. {
  4. $dbhost = "***";
  5. $dbuser = "***";
  6. $dbpass = "***";
  7. $dbname = "***";
  8. // Create connection
  9. $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);
  10.  
  11. return $conn;
  12. }
  13. //Checks if there is a login cookie
  14. if(isset($_COOKIE['ID_your_site'])){ //if there is, it logs you in and directes you to the members page
  15. $username = $_COOKIE['ID_your_site'];
  16. $pass = $_COOKIE['Key_your_site'];
  17. $check = mysqli_query($conn, "SELECT * FROM users WHERE username = '$username'")or die(mysqli_error());
  18.  
  19. while($info = mysqli_fetch_array( $check )){
  20. if ($pass != $info['password']){}
  21. else{
  22. header("Location: login.php");
  23. }
  24. }
  25. }
  26.  
  27. //if the login form is submitted
  28. if (isset($_POST['submit'])) {
  29.  
  30. // makes sure they filled it in
  31. if(!$_POST['username']){
  32. die('You did not fill in a username.');
  33. }
  34. if(!$_POST['pass']){
  35. die('You did not fill in a password.');
  36. }
  37.  
  38. // checks it against the database
  39. if (!get_magic_quotes_gpc()){
  40. $_POST['email'] = addslashes($_POST['email']);
  41. }
  42.  
  43. $check = mysqli_query($conn, "SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysqli_error());
  44.  
  45. //Gives error if user dosen't exist
  46. $check2 = mysqli_num_rows($check);
  47. if ($check2 == 0){
  48. die('That user does not exist in our database.<br /><br />If you think this is wrong <a href="login.php">try again</a>.');
  49. }
  50.  
  51. while($info = mysqli_fetch_array( $check )){
  52. $_POST['pass'] = stripslashes($_POST['pass']);
  53. $info['password'] = stripslashes($info['password']);
  54. $_POST['pass'] = md5($_POST['pass']);
  55.  
  56. //gives error if the password is wrong
  57. if ($_POST['pass'] != $info['password']){
  58. die('Incorrect password, please <a href="login.php">try again</a>.');
  59. }
  60.  
  61. else{ // if login is ok then we add a cookie
  62. $_POST['username'] = stripslashes($_POST['username']);
  63. $hour = time() + 3600;
  64. setcookie(ID_your_site, $_POST['username'], $hour);
  65. setcookie(Key_your_site, $_POST['pass'], $hour);
  66.  
  67. //then redirect them to the members area
  68. header("Location: members.php");
  69. }
  70. }
  71. }
  72. else{
  73. // if they are not logged in
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement