Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. //try to login using cookies, but first make sure it doesn't have a $_SESSION variable for it
  2. if (!(isset($_SESSION['valid_user']))) {
  3. if (isset($_COOKIE["login"])) {
  4. //try to login using cookie
  5. $query = "select from cookie_users where uniqueid = '".$_COOKIE["login"]."')";
  6. $conn = new mysqli('localhost', 'user', 'password', 'test');
  7. $result = $conn->query($query);
  8. if ($result->num_rows>0) {
  9. $result->fetch_assoc();
  10. $result['username'] = $username;
  11. $result['password'] = $password;
  12. //try to login using password and username from cookie database
  13. $query = "select * from authorized_users where username = '".$username."' and password = '".$password."'";
  14. $result2 = $conn->query($query);
  15. if ($result->num_rows>0) {
  16. $_SESSION['valid_user'] = $username;
  17. }
  18. }
  19. }
  20. }
  21.  
  22. if (isset($_POST['userid']) && isset($_POST['password'])) {
  23. // if the user has just tried to log in
  24. $userid = $_POST['userid'];
  25. $password = $_POST['password'];
  26.  
  27. $db_conn=new mysqli('localhost', 'user', 'password', 'test');
  28.  
  29. if (mysqli_connect_errno()) {
  30. echo 'Connection to database failed: '.mysqli_connect_errno();
  31. exit;
  32. }
  33. $query = 'select * from authorized_users '."where name='$userid' "."and password=sha1('$password')";
  34.  
  35. $result = $db_conn->query($query);
  36. if ($result->num_rows > 0) {
  37. //if they are in the database, register the user id
  38. $_SESSION['valid_user'] = $userid;
  39. //set up cookie
  40. setcookie("login", $uniqueid, time()*60*60*24*14);
  41. $query = "insert into cookie_users values ('".$userid."', sha1('".$password."'), '".$uniqueid."')";
  42. $result = $db_conn->query($query);
  43. if (!$result) {
  44. echo 'Could not update cookie in database';
  45. }
  46. }
  47. $db_conn->close();
  48. }
  49.  
  50. if (isset($_SESSION['valid_user'] {
  51. echo "members only content goes here";
  52. } else {
  53. echo "you need to login"; }
  54.  
  55. <?php
  56.  
  57. setcookie("test", "the test is good", time()*60*60);
  58.  
  59. ?>
  60.  
  61. <html>
  62. <body>
  63. <?php
  64. echo $_COOKIE["test"];
  65. ?>
  66. </body>
  67. </html>
  68.  
  69. setcookie("login", $uniqueid, time()*60*60*24*14);
  70.  
  71. setcookie("login", $uniqueid, time() + 60*60*24*14); // Adding two weeks, not multipying by >9000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement