Advertisement
Guest User

login

a guest
Apr 19th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. <?php
  2. function register(){
  3. $user = filter_input(INPUT_POST, 'user');
  4. $pass = filter_input(INPUT_POST, 'pass');
  5. $query = 'INSERT INTO usrlgin
  6. (user_name, user_pass)
  7. VALUES
  8. (:user, :pass)';
  9. $statement = $db->prepare($query);
  10. $statement->bindValue(':user', $user);
  11. $statement->bindValue(':pass', $pass);
  12. $statement->execute();
  13. $statement->closeCursor();
  14. }
  15. if (isset($_COOKIE['user_id']))
  16. {
  17. header("location:display.php");
  18. }
  19. elseif(!isset( $_POST['user'], $_POST['pass']))
  20. {
  21. $message = 'Please enter a valid username and password';
  22. }
  23. elseif(empty( $_POST['user'])||empty ( $_POST['pass']))
  24. {
  25. $message = 'Please enter a valid username and password';
  26. }
  27. else
  28. {
  29. try
  30. {
  31. require_once '../database.php';
  32. $user = filter_input(INPUT_POST, 'user');
  33. $pass = filter_input(INPUT_POST, 'pass');
  34. try
  35. {
  36. $query1 = 'SELECT * FROM usrlgin
  37. WHERE user_name=:user AND user_pass=:pass';
  38. $statement1 = $db->prepare($query1);
  39. $statement1->bindValue(':user', $user);
  40. $statement1->bindValue(':pass', $pass);
  41. $statement1->execute();
  42. $user_id = $statement1->fetchColumn();
  43. $statement1->closeCursor();
  44.  
  45.  
  46.  
  47. if($user_id == false)
  48. {
  49. $message = "Login Failed";
  50.  
  51. }
  52. else
  53. {
  54. $randid = rand();
  55. $query2 = 'SELECT cookieID FROM usrlgin';
  56. $statement2 = $db->prepare($query2);
  57. $statement2->execute();
  58. $checkrand = $statement2->fetchAll();
  59. $statement2->closeCursor();
  60. foreach ($checkrand as $check) {
  61. while($randid == $check){
  62. $randid = rand();
  63. }
  64. }
  65. $query3 = 'UPDATE usrlgin
  66. SET cookieID=:rand
  67. WHERE ID=:id';
  68. $statement3 = $db->prepare($query3);
  69. $statement3->bindValue(':rand', 'user_id'.$randid);
  70. $statement3->bindValue(':id', $user_id);
  71. $statement3->execute();
  72. $statement3->closeCursor();
  73.  
  74. setcookie('user_id', 'user_id'.$randid , 0, '/');
  75. header("location:display.php");
  76. }
  77.  
  78.  
  79. }
  80. catch(Exception $e)
  81. {
  82.  
  83. $message = 'Error';
  84. }
  85. }
  86. catch(Exception $e)
  87. {
  88.  
  89. $message = 'Please enter Username and Password';
  90. }
  91. }
  92.  
  93. ?>
  94. <!DOCTYPE html>
  95. <html>
  96. <head>
  97. <meta charset="UTF-8">
  98. <title>Login</title>
  99. <link rel="stylesheet" type="text/css" href="../../css/main.css">
  100. </head>
  101. <body>
  102. <?php
  103. // put your code here
  104. ?>
  105. <header>
  106. <h1>Login</h1>
  107. </header>
  108. <aside class="index">
  109. <h1>sidebar</h1>
  110. <h3><a href="../../index.php">Back</a></h3>
  111. </aside>
  112. <section class="index">
  113. <form action="#" method="post">
  114. <ul>
  115. <li><input type="text" name="user"></li><br>
  116. <li><input type="text" name="pass"></li><br>
  117. <li><input type="submit" value="Login"></li>
  118. </ul>
  119. </form>
  120. <h2><?php echo $message; ?></h2>
  121. </section>
  122. <footer>
  123. <p>&#169Aaron Swede-Taillon 2016</p>
  124. </footer>
  125. </body>
  126. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement