Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. HOMEPAGE ONCE LOGGED IN -->
  2.  
  3. <?php
  4. require 'user.php';
  5. $fetch = new Connect();
  6. session_start();
  7. Session::get('user');
  8.  
  9. if (!Session::get('user')) {
  10. echo "Session start didn't work.";
  11. }else {
  12. echo 'worked fine.';
  13. }
  14.  
  15. ?>
  16.  
  17. < -- ENDS --
  18.  
  19.  
  20.  
  21. INDEX PAGE -->
  22. <?php
  23. require 'user.php';
  24.  
  25.  
  26. if(isset($_POST['submit']))
  27. {
  28. $name = $_POST['user'];
  29. $pass = $_POST['password'];
  30.  
  31. $object = new Connect();
  32. $object->login($name, $pass);
  33. }
  34.  
  35. if(isset($_POST['register']))
  36. {
  37. $name = $_POST['reg_user'];
  38. $pass = $_POST['reg_pass'];
  39.  
  40. $reg = new Connect();
  41. $reg->register($name, $pass);
  42. }
  43. ?>
  44.  
  45. <!-- LOGIN STARTS -->
  46. <h2>Login</h2>
  47. <form action="" method="POST">
  48. Username: <input type="text" name="user">
  49. Password: <input type="password" name="password">
  50. <input type="submit" name="submit" value="Login">
  51. </form>
  52. <!-- LOGIN ENDS -->
  53.  
  54.  
  55. <!-- REGISTER STARTS -->
  56. <h2>Register</h2>
  57. <form action="" method="POST">
  58. Username: <input type="text" name="reg_user">
  59. Password: <input type="text" name="reg_pass">
  60. <input type="submit" name="register" value="Register">
  61. </form>
  62.  
  63. <-- INDEX PAGE ENDS
  64.  
  65.  
  66.  
  67. USER CLASSES FILE STARTS -->
  68.  
  69. <?php
  70. class Session {
  71. public static function set($key, $name)
  72. {
  73. $this->session = Session::start('user');
  74. $_SESSION[$key] = $name;
  75. }
  76.  
  77. public static function get($key)
  78. {
  79. if (isset($_SESSION[$key])){
  80. return $_SESSION[$key];
  81. }
  82. else{
  83. return false;
  84. }
  85. }
  86. }
  87. class Connect
  88. {
  89.  
  90. public function __construct()
  91. {
  92. try
  93. {
  94. $this->db = new PDO("mysql:host=127.0.0.1;dbname=testing",'root','root');
  95. }
  96. catch(PDOException $e)
  97. {
  98. echo $e->getMessage();
  99. }
  100. }
  101.  
  102. public function login($name, $pass)
  103. {
  104. if(!empty($name) && !empty($pass))
  105. {
  106.  
  107. $st = $this->db->prepare("SELECT password FROM users WHERE username= ?");
  108. $st->bindParam(1, $name);
  109. $st->execute();
  110.  
  111. if($st->rowCount() == 1)
  112. {
  113. $db_pass = $st->fetch();
  114. if(password_verify($pass, $db_pass['password']))
  115. {
  116. $this->session;
  117. header ('Location: homepage.php');
  118. }
  119. else
  120. {
  121. echo "incorrect username or password.";
  122. }
  123. }
  124. else
  125. {
  126. echo "incorrect username or password.";
  127. }
  128. }
  129. else
  130. {
  131. echo "Please supply a username and password.";
  132. }
  133. }
  134. public function register($name, $pass)
  135. {
  136. if(!empty($name) && !empty($pass))
  137. {
  138. $get = $this->db->prepare("SELECT * FROM users WHERE username = ?");
  139. $get->bindParam(1, $name);
  140. $get->execute();
  141.  
  142. if($get->rowCount() == 0)
  143. {
  144. $pass = password_hash($pass, PASSWORD_BCRYPT);
  145. echo $pass;
  146. $set = $this->db->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
  147. $set->bindParam(1, $name);
  148. $set->bindParam(2, $pass);
  149. $set->execute();
  150.  
  151. echo "Your details have now been registered.";
  152. }
  153. else
  154. {
  155. echo "An account with this username already exists.";
  156. }
  157. }
  158. else
  159. {
  160. echo "Please fill in all of the fields.";
  161. }
  162. }
  163. }
  164.  
  165. ?>
  166. <-- USER PAGE ENDS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement