Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.55 KB | None | 0 0
  1. <?php
  2.  
  3. // database Connection
  4. function DB()
  5. {
  6. try {
  7. $db = new PDO("sqlite:db/login_system.db");
  8. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. return $db;
  10. } catch (PDOException $e) {
  11. return "Error!: " . $e->getMessage();
  12. die();
  13. }
  14. }
  15.  
  16. ?>
  17.  
  18. <?php
  19.  
  20. class DemoLib
  21. {
  22.  
  23. /*
  24. * Register New User
  25. *
  26. * @param $name, $email, $username, $password
  27. * @return ID
  28. * */
  29. public function Register($name, $email, $username, $password)
  30. {
  31. try {
  32. //$db = DB();
  33. $db = new PDO("sqlite:db/login_system.db");
  34. $insert = "INSERT INTO users(name, email, username, password) VALUES (:name,:email,:username,:password)";
  35. $query = $db->prepare($insert);
  36. $query->bindParam("name", $name, PDO::PARAM_STR);
  37. $query->bindParam("email", $email, PDO::PARAM_STR);
  38. $query->bindParam("username", $username, PDO::PARAM_STR);
  39. $enc_password = hash('sha256', $password);
  40. $query->bindParam("password", $enc_password, PDO::PARAM_STR);
  41. $query->execute();
  42. return $db->lastInsertId();
  43. } catch (PDOException $e) {
  44. exit($e->getMessage());
  45. }
  46. }
  47.  
  48. /*
  49. * Check Username
  50. *
  51. * @param $username
  52. * @return boolean
  53. * */
  54. public function isUsername($username)
  55. {
  56.  
  57. try {
  58. //$db = DB();
  59. $db = new PDO("sqlite:db/login_system.db");
  60. $select = "SELECT user_id FROM users WHERE username=:username";
  61. $query = $db->prepare($select);
  62. $query->bindParam("username", $username, PDO::PARAM_STR);
  63. $query->execute();
  64. if ($query->rowCount() > 0) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. } catch (PDOException $e) {
  70. exit($e->getMessage());
  71. }
  72. }
  73.  
  74. /*
  75. * Check Email
  76. *
  77. * @param $email
  78. * @return boolean
  79. * */
  80. public function isEmail($email)
  81. {
  82.  
  83. try {
  84. //$db = DB();
  85. $db = new PDO("sqlite:db/login_system.db");
  86. $select = "SELECT user_id FROM users WHERE email=:email";
  87. $query = $db->prepare($select);
  88. $query->bindParam("email", $email, PDO::PARAM_STR);
  89. $query->execute();
  90. if ($query->rowCount() > 0) {
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. } catch (PDOException $e) {
  96. exit($e->getMessage());
  97. }
  98. }
  99.  
  100. /*
  101. * Login
  102. *
  103. * @param $username, $password
  104. * @return $mixed
  105. * */
  106. public function Login($username, $password)
  107. {
  108. try {
  109. //$db = DB();
  110. $db = new PDO("sqlite:db/login_system.db");
  111. $select = "SELECT user_id FROM users WHERE username=:username AND password=:password";
  112. $query = $db->prepare($select);
  113. $query->bindParam("username", $username, PDO::PARAM_STR);
  114. $enc_password = hash('sha256', $password);
  115. $query->bindParam("password", $enc_password, PDO::PARAM_STR);
  116. $query->execute();
  117. if ($query->rowCount() > 0) {
  118. $result = $query->fetch(PDO::FETCH_OBJ);
  119. return $result->user_id;
  120. } else {
  121. return false;
  122. }
  123. } catch (PDOException $e) {
  124. exit($e->getMessage());
  125. }
  126. }
  127.  
  128. /*
  129. * get User Details
  130. *
  131. * @param $user_id
  132. * @return $mixed
  133. * */
  134. public function UserDetails($user_id)
  135. {
  136. try {
  137. //$db = DB();
  138. $db = new PDO("sqlite:db/login_system.db");
  139. $select = "SELECT user_id, name, username, email FROM users WHERE user_id=:user_id";
  140. $query = $db->prepare($select);
  141. $query->bindParam("user_id", $user_id, PDO::PARAM_STR);
  142. $query->execute();
  143. if ($query->rowCount() > 0) {
  144. return $query->fetch(PDO::FETCH_OBJ);
  145. }
  146. } catch (PDOException $e) {
  147. exit($e->getMessage());
  148. }
  149. }
  150. }
  151.  
  152. <?php
  153.  
  154. include("database.php");//make connection here
  155.  
  156. // Application library ( with DemoLib class )
  157. //require __DIR__ . '/lib/library.php';
  158. include("lib/library.php");
  159. $app = new DemoLib();
  160.  
  161. $login_error_message = '';
  162. $register_error_message = '';
  163.  
  164. // check Register request
  165. if (!empty($_POST['join'])) {
  166. if ($_POST['name'] == "") {
  167. $register_error_message = 'Name field is required!';
  168. } else if ($_POST['email'] == "") {
  169. $register_error_message = 'Email field is required!';
  170. } else if ($_POST['username'] == "") {
  171. $register_error_message = 'Username field is required!';
  172. } else if ($_POST['password'] == "") {
  173. $register_error_message = 'Password field is required!';
  174. } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  175. $register_error_message = 'Invalid email address!';
  176. } else if ($app->isEmail($_POST['email'])) {
  177. $register_error_message = 'Email is already in use!';
  178. } else if ($app->isUsername($_POST['username'])) {
  179. $register_error_message = 'Username is already in use!';
  180. } else {
  181. $user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['username'], $_POST['password']);
  182. // set session and redirect user to the welcome page
  183. $_SESSION['user_id'] = $user_id;
  184. header("Location: login.php");
  185. }
  186. }
  187.  
  188. ?>
  189.  
  190. <!DOCTYPE html>
  191.  
  192. <html >
  193. <head>
  194. <meta charset="UTF-8">
  195. <title>Daily UI 001 Sign Up Form</title>
  196.  
  197.  
  198. <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.css'>
  199. <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
  200.  
  201. <link rel="stylesheet" href="css/style.css">
  202.  
  203. </head>
  204.  
  205. <body>
  206. <div class="signupSection">
  207. <div class="info">
  208. <h2>Sign Up</h2>
  209. <i class="icon ion-ios-ionic-outline" aria-hidden="true"></i>
  210. <p>The Future Is Here</p>
  211. </div>
  212.  
  213. <?php
  214. if ($register_error_message != "") {
  215. echo '<script language="javascript">alert("Please enter valid fields required");</script>';
  216. }
  217. ?>
  218.  
  219. <form action="registration.php" method="POST" class="signupForm" name="signupform">
  220.  
  221. <ul class="noBullet">
  222. <li>
  223. <label for="name"></label>
  224. <input type="text" class="inputFields" id="name" name="name" placeholder="Name" value="" oninput="return userNameValidation(this.value)"/>
  225. </li>
  226. <li>
  227. <label for="username"></label>
  228. <input type="text" class="inputFields" id="username" name="username" placeholder="Username" value="" oninput="return userNameValidation(this.value)" required/>
  229. </li>
  230. <li>
  231. <label for="password"></label>
  232. <input type="password" class="inputFields" id="password" name="password" placeholder="Password" value="" oninput="return passwordValidation(this.value)" required/>
  233. </li>
  234. <li>
  235. <label for="email"></label>
  236. <input type="email" class="inputFields" id="email" name="email" placeholder="Email" value="" required/>
  237. </li>
  238. <li id="center-btn">
  239. <input type="submit" id="join-btn" name="join" alt="Join" value="Join">
  240. <input type="button" id="join-btn" name="join" alt="Join" value="Already registered" onclick="window.location='login.php';">
  241. </li>
  242. </ul>
  243. </form>
  244. </div>
  245.  
  246. <script src="js/index.js"></script>
  247.  
  248. </body>
  249. </html>
  250.  
  251. <?php
  252. session_start();//session starts here
  253.  
  254. include("database.php");
  255.  
  256. // Application library ( with DemoLib class )
  257. //require __DIR__ . '/lib/library.php';
  258. include("lib/library.php");
  259. $app = new DemoLib();
  260.  
  261. $login_error_message = '';
  262. $register_error_message = '';
  263.  
  264. // check Login request
  265. if (!empty($_POST['join'])) {
  266.  
  267. $username = trim($_POST['username']);
  268. $password = trim($_POST['password']);
  269.  
  270. if ($username == "") {
  271. $login_error_message = 'Username field is required!';
  272. } else if ($password == "") {
  273. $login_error_message = 'Password field is required!';
  274. } else {
  275. $user_id = $app->Login($username, $password); // check user login
  276. if($user_id > 0)
  277. {
  278. $_SESSION['user_id'] = $user_id; // Set Session
  279.  
  280. header("Location: welcome.php"); // Redirect user to the welcome.php
  281. }
  282. else
  283. {
  284. $login_error_message = 'Invalid login details!';
  285. }
  286. }
  287. }
  288.  
  289. ?>
  290. <!DOCTYPE html>
  291. <html>
  292. <head>
  293. <meta charset="UTF-8">
  294. <title>Daily UI 001 Sign Up Form</title>
  295.  
  296.  
  297. <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.css'>
  298. <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
  299.  
  300. <link rel="stylesheet" href="css/style.css">
  301.  
  302. </head>
  303.  
  304. <body>
  305. <div class="signupSection">
  306. <div class="info">
  307. <h2>Login-Logout using PHP & SQLite-PDO</h2>
  308. <i class="icon ion-ios-ionic-outline" aria-hidden="true"></i>
  309. <p>The Future Is Here</p>
  310. </div>
  311. <?php
  312. if ($login_error_message != "") {
  313. echo '<script language="javascript">alert("Please enter valid username and password");</script>';
  314. }
  315. ?>
  316. <form action="login.php" method="POST" class="signupForm" name="signupform">
  317. <h2>Log In</h2>
  318. <ul class="noBullet">
  319. <li>
  320. <label for="username"></label>
  321. <input type="text" class="inputFields" id="username" name="username" placeholder="Username" value="" oninput="return userNameValidation(this.value)" required/>
  322. </li>
  323. <li>
  324. <label for="password"></label>
  325. <input type="password" class="inputFields" id="password" name="password" placeholder="Password" value="" oninput="return passwordValidation(this.value)" required/>
  326. </li>
  327. <li id="center-btn">
  328. <input type="submit" id="join-btn" name="join" alt="Join" value="Log In">
  329. <input type="button" id="join-btn" name="join" alt="Join" value="Create Account" onclick="window.location='registration.php';">
  330. </li>
  331. </ul>
  332. </form>
  333. </div>
  334.  
  335. <script src="js/index.js"></script>
  336.  
  337. </body>
  338. </html>
  339.  
  340. <?php
  341.  
  342. // Start Session
  343. session_start();
  344.  
  345. // check user login
  346. if(empty($_SESSION['user_id']))
  347. {
  348. header("Location: login.php");
  349. }
  350.  
  351. // Database connection
  352. require __DIR__ . '/database.php';
  353. $db = DB();
  354.  
  355. // Application library ( with DemoLib class )
  356. //require __DIR__ . '/lib/library.php';
  357. include("lib/library.php");
  358. $app = new DemoLib();
  359.  
  360. $user = $app->UserDetails($_SESSION['user_id']); // get user details
  361.  
  362. ?>
  363.  
  364. <html>
  365. <head>
  366.  
  367. <title>
  368. Registration
  369. </title>
  370. <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.css'>
  371. <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
  372.  
  373. <link rel="stylesheet" href="css/style.css">
  374.  
  375. </head>
  376.  
  377. <body>
  378. <div class="signupSection">
  379. <div class="info">
  380. <h2>Welcome</h2>
  381.  
  382. <?php
  383.  
  384. echo $user->name
  385.  
  386. ?>
  387.  
  388. </div>
  389. <br>
  390. <div class="signupForm">
  391. <ul class="noBullet">
  392. <li id="center-btn">
  393. <input type="button" id="join-btn" name="join" alt="Join" value="Users" onclick="window.location='view_users.php';">
  394. <input type="button" id="join-btn" name="join" alt="Join" value="Log out" onclick="window.location='logout.php';">
  395. </li>
  396. </ul>
  397. </div>
  398.  
  399. </div>
  400. </body>
  401.  
  402. </html>
  403.  
  404. <?php
  405.  
  406. // start session
  407. session_start();
  408.  
  409. // Destroy user session
  410. unset($_SESSION['user_id']);
  411.  
  412. // Redirect to index.php page
  413. header("Location: login.php");
  414. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement