Guest User

Untitled

a guest
Sep 7th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  2. SET time_zone = "+00:00";
  3.  
  4. -- Database: `login`
  5.  
  6. -- Table structure for table `users`
  7. CREATE TABLE IF NOT EXISTS `users` (
  8. `user_id` int(11) NOT NULL AUTO_INCREMENT,
  9. `user_name` varchar(15) NOT NULL,
  10. `user_email` varchar(40) NOT NULL,
  11. `user_pass` varchar(255) NOT NULL,
  12. `joining_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  13. PRIMARY KEY (`user_id`)
  14. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  15.  
  16. <?php
  17. // Other settings
  18. session_start();
  19.  
  20. // Connect to the database
  21. class Database {
  22. private $host = "localhost";
  23. private $db_name = "login";
  24. private $username = "root";
  25. private $password = "";
  26. public $conn;
  27.  
  28. public function dbConnection() {
  29. $this->conn = null;
  30. try {
  31. $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  32. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  33. }
  34. catch(PDOException $exception) {
  35. echo "Connection error: " . $exception->getMessage();
  36. }
  37. return $this->conn;
  38. }
  39. }
  40.  
  41. // Functions for managing users
  42. class USER {
  43. private $conn;
  44. public function __construct() {
  45. $database = new Database();
  46. $db = $database->dbConnection();
  47. $this->conn = $db;
  48. }
  49.  
  50. public function runQuery($sql) {
  51. $stmt = $this->conn->prepare($sql);
  52. return $stmt;
  53. }
  54.  
  55. public function register($uname,$umail,$upass) {
  56. try {
  57. $new_password = password_hash($upass, PASSWORD_DEFAULT);
  58. $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass) VALUES(:uname, :umail, :upass)");
  59. $stmt->bindparam(":uname", $uname);
  60. $stmt->bindparam(":umail", $umail);
  61. $stmt->bindparam(":upass", $new_password);
  62. $stmt->execute();
  63. return $stmt;
  64. }
  65. catch(PDOException $e) {
  66. echo $e->getMessage();
  67. }
  68. }
  69.  
  70. public function doLogin($uname,$umail,$upass) {
  71. try {
  72. $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM users WHERE user_name=:uname OR user_email=:umail ");
  73. $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
  74. $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
  75. if($stmt->rowCount() == 1) {
  76. if(password_verify($upass, $userRow['user_pass'])) {
  77. $_SESSION['user_session'] = $userRow['user_id'];
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. }
  84. catch(PDOException $e) {
  85. echo $e->getMessage();
  86. }
  87. }
  88.  
  89. public function is_loggedin() {
  90. if(isset($_SESSION['user_session'])) {
  91. return true;
  92. }
  93. }
  94.  
  95. public function redirect($url) {
  96. header("Location: $url");
  97. exit;
  98. }
  99.  
  100. public function doLogout() {
  101. unset($_SESSION['user_session']);
  102. return true;
  103. }
  104. }
  105. ?>
  106.  
  107. <?php
  108. require_once('assets/config.php');
  109. // If you are not logged, redirects to login page.
  110. $session = new USER();
  111. if(!$session->is_loggedin()) {$session->redirect('login.php');}
  112.  
  113. $auth_user = new USER();
  114. $user_id = $_SESSION['user_session'];
  115. $stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
  116. $stmt->execute(array(":user_id"=>$user_id));
  117. $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
  118. ?>
  119. <html>
  120. <head>
  121. <meta charset="utf-8">
  122. <title>Welcome</title>
  123. <link href="assets/styles.css" rel="stylesheet">
  124. </head>
  125. <body>
  126. <div class="container">
  127. <h1>Hello, <?php echo $userRow['user_name'];?>! - <a href="logout.php">Logout</a></h1>
  128. <hr/>
  129. <p>This is the user area, this content is private.</p>
  130. </div>
  131. </body>
  132. </html>
  133.  
  134. <?php
  135. require_once('assets/config.php');
  136. $login = new USER();
  137.  
  138. if($login->is_loggedin()!="") {
  139. $login->redirect('index.php');
  140. }
  141.  
  142. if(isset($_POST['btn-login'])) {
  143. $uname = strip_tags($_POST['txt_uname_email']);
  144. $umail = strip_tags($_POST['txt_uname_email']);
  145. $upass = strip_tags($_POST['txt_password']);
  146.  
  147. if($login->doLogin($uname,$umail,$upass)) {
  148. $login->redirect('index.php');
  149. } else {
  150. $error = "Wrong Details!";
  151. }
  152. }
  153. ?>
  154. <html>
  155. <head>
  156. <meta charset="utf-8">
  157. <title>Login</title>
  158. <link href="assets/styles.css" rel="stylesheet">
  159. </head>
  160. <body>
  161. <div class="container">
  162. <h1>Login or <a href="register.php">Register</a></h1>
  163. <hr/>
  164. <div class="error">
  165. <?php
  166. if(isset($error)) {
  167. echo "<p class='error'>$error</p>";
  168. }
  169. if(isset($_GET['joined'])) {
  170. echo "<p class='success'>Successfully registered please login</p>";
  171. }
  172. ?>
  173. </div>
  174. <form method="post" id="login-form">
  175. <input type="text" name="txt_uname_email" placeholder="Username or Email"/>
  176. <input type="password" name="txt_password" placeholder="Password" />
  177. <button type="submit" name="btn-login">Login</button>
  178. </form>
  179. </div>
  180. </body>
  181. </html>
  182.  
  183. <?php
  184. require_once('assets/config.php');
  185. $user = new USER();
  186.  
  187. if($user->is_loggedin()!="") {
  188. $user->redirect('index.php');
  189. }
  190.  
  191. if(isset($_POST['btn-signup'])) {
  192. $uname = strip_tags($_POST['txt_uname']);
  193. $umail = strip_tags($_POST['txt_umail']);
  194. $upass = strip_tags($_POST['txt_upass']);
  195.  
  196. if($uname=="") {
  197. $error[] = "Provide username!";
  198. }
  199. else if($umail=="") {
  200. $error[] = "Provide email!";
  201. }
  202. else if(!filter_var($umail, FILTER_VALIDATE_EMAIL)) {
  203. $error[] = 'Please enter a valid email address!';
  204. }
  205. else if($upass=="") {
  206. $error[] = "Provide password!";
  207. }
  208. else if(strlen($upass) < 6){
  209. $error[] = "Password must be atleast 6 characters!";
  210. } else {
  211. try {
  212. $stmt = $user->runQuery("SELECT user_name, user_email FROM users WHERE user_name=:uname OR user_email=:umail");
  213. $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
  214. $row=$stmt->fetch(PDO::FETCH_ASSOC);
  215.  
  216. if($row['user_name']==$uname) {
  217. $error[] = "Sorry username already taken!";
  218. } else if($row['user_email']==$umail) {
  219. $error[] = "Sorry email id already taken!";
  220. } else {
  221. if($user->register($uname,$umail,$upass)){
  222. $user->redirect('login.php?joined');
  223. }
  224. }
  225. }
  226. catch(PDOException $e) {
  227. echo $e->getMessage();
  228. }
  229. }
  230. }
  231. ?>
  232. <html>
  233. <head>
  234. <meta charset="utf-8">
  235. <title>Register</title>
  236. <link href="assets/styles.css" rel="stylesheet">
  237. </head>
  238. <body>
  239. <div class="container">
  240. <h1>Register or <a href="login.php">Login</a></h1>
  241. <hr/>
  242. <?php
  243. if(isset($error)) {
  244. foreach($error as $error) {
  245. echo "<p class='error'>$error</p>";
  246. }
  247. }
  248. ?>
  249. <form method="post">
  250. <input type="text" name="txt_uname" placeholder="Username" value="<?php if(isset($error)){echo $uname;}?>" />
  251. <input type="text" name="txt_umail" placeholder="Email" value="<?php if(isset($error)){echo $umail;}?>" />
  252. <input type="password" name="txt_upass" placeholder="Password" />
  253. <button type="submit" name="btn-signup">Register</button>
  254. </form>
  255. </div>
  256. </body>
  257. </html>
  258.  
  259. <?php
  260. require_once('assets/config.php');
  261.  
  262. $user_logout = new USER();
  263. $user_logout->doLogout();
  264. $user_logout->redirect('index.php');
  265. ?>
Add Comment
Please, Sign In to add comment