Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.71 KB | None | 0 0
  1. <?php
  2.  
  3. // database Connection variables
  4. define('HOST', 'localhost'); // Database host name ex. localhost
  5. define('USER', 'root'); // Database user. ex. root ( if your on local server)
  6. define('PASSWORD', ''); // user password (if password is not set for user then keep it empty )
  7. define('DATABASE', 'databasename'); // Database Database name
  8.  
  9. function DB()
  10. {
  11. try {
  12. $db = new PDO('mysql:host='.HOST.';dbname='.DATABASE.'', USER, PASSWORD);
  13. return $db;
  14. } catch (PDOException $e) {
  15. return "Error!: " . $e->getMessage();
  16. die();
  17. }
  18. }
  19. ?>
  20.  
  21. <div class="row">
  22. <div class="col-md-5 well">
  23. <h4>Login</h4>
  24. <?php
  25. if ($login_error_message != "") {
  26. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $login_error_message . '</div>';
  27. }
  28. ?>
  29. <form action="index.php" method="post">
  30. <div class="form-group">
  31. <label for="">Username/Email</label>
  32. <input type="text" name="username" class="form-control"/>
  33. </div>
  34. <div class="form-group">
  35. <label for="">Password</label>
  36. <input type="password" name="password" class="form-control"/>
  37. </div>
  38. <div class="form-group">
  39. <input type="submit" name="btnLogin" class="btn btn-primary" value="Login"/>
  40. </div>
  41. </form>
  42. </div>
  43. </div>
  44.  
  45. <div class="row">
  46. <div class="col-md-5 well">
  47. <h4>Register</h4>
  48. <?php
  49. if ($register_error_message != "") {
  50. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $register_error_message . '</div>';
  51. }
  52. ?>
  53. <form action="index.php" method="post">
  54. <div class="form-group">
  55. <label for="">Name</label>
  56. <input type="text" name="name" class="form-control"/>
  57. </div>
  58. <div class="form-group">
  59. <label for="">Email</label>
  60. <input type="email" name="email" class="form-control"/>
  61. </div>
  62. <div class="form-group">
  63. <label for="">Username</label>
  64. <input type="text" name="username" class="form-control"/>
  65. </div>
  66. <div class="form-group">
  67. <label for="">Password</label>
  68. <input type="password" name="password" class="form-control"/>
  69. </div>
  70. <div class="form-group">
  71. <input type="submit" name="btnRegister" class="btn btn-primary" value="Register"/>
  72. </div>
  73. </form>
  74. </div>
  75. </div>
  76.  
  77. <?php
  78.  
  79. // Start Session
  80. session_start();
  81.  
  82. // Database connection
  83. require __DIR__ . '/database.php';
  84. $db = DB();
  85.  
  86. // Application library ( with DemoLib class )
  87. require __DIR__ . '/lib/library.php';
  88. $app = new DemoLib();
  89.  
  90. $login_error_message = '';
  91. $register_error_message = '';
  92.  
  93. // check Login request
  94. if (!empty($_POST['btnLogin'])) {
  95.  
  96. $username = trim($_POST['username']);
  97. $password = trim($_POST['password']);
  98.  
  99. if ($username == "") {
  100. $login_error_message = 'Username field is required!';
  101. } else if ($password == "") {
  102. $login_error_message = 'Password field is required!';
  103. } else {
  104. $user_id = $app->Login($username, $password); // check user login
  105. if($user_id > 0)
  106. {
  107. $_SESSION['user_id'] = $user_id; // Set Session
  108. header("Location: profile.php"); // Redirect user to the profile.php
  109. }
  110. else
  111. {
  112. $login_error_message = 'Invalid login details!';
  113. }
  114. }
  115. }
  116.  
  117. // check Register request
  118. if (!empty($_POST['btnRegister'])) {
  119. if ($_POST['name'] == "") {
  120. $register_error_message = 'Name field is required!';
  121. } else if ($_POST['email'] == "") {
  122. $register_error_message = 'Email field is required!';
  123. } else if ($_POST['username'] == "") {
  124. $register_error_message = 'Username field is required!';
  125. } else if ($_POST['password'] == "") {
  126. $register_error_message = 'Password field is required!';
  127. } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  128. $register_error_message = 'Invalid email address!';
  129. } else if ($app->isEmail($_POST['email'])) {
  130. $register_error_message = 'Email is already in use!';
  131. } else if ($app->isUsername($_POST['username'])) {
  132. $register_error_message = 'Username is already in use!';
  133. } else {
  134. $user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['username'], $_POST['password']);
  135. // set session and redirect user to the profile page
  136. $_SESSION['user_id'] = $user_id;
  137. header("Location: profile.php");
  138. }
  139. }
  140. ?>
  141. <!doctype html>
  142. <html lang="en">
  143. <head>
  144. <meta charset="UTF-8">
  145. <title>Home</title>
  146. <!-- Latest compiled and minified CSS -->
  147. <link rel="stylesheet" href="css/bootstrap.min.css">
  148. </head>
  149. <body>
  150.  
  151. <div class="container">
  152. <div class="row">
  153. <div class="col-md-12">
  154. <h2>
  155. PHP Login Registration System with PDO Connection using SHA-256 Cryptographic Hash Algorithm to store Password
  156. </h2>
  157. </div>
  158. </div>
  159. <div class="form-group">
  160. Note: .
  161. </div>
  162. <div class="row">
  163. <div class="col-md-5 well">
  164. <h4>Register</h4>
  165. <?php
  166. if ($register_error_message != "") {
  167. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $register_error_message . '</div>';
  168. }
  169. ?>
  170. <form action="index.php" method="post">
  171. <div class="form-group">
  172. <label for="">Name</label>
  173. <input type="text" name="name" class="form-control"/>
  174. </div>
  175. <div class="form-group">
  176. <label for="">Email</label>
  177. <input type="email" name="email" class="form-control"/>
  178. </div>
  179. <div class="form-group">
  180. <label for="">Username</label>
  181. <input type="text" name="username" class="form-control"/>
  182. </div>
  183. <div class="form-group">
  184. <label for="">Password</label>
  185. <input type="password" name="password" class="form-control"/>
  186. </div>
  187. <div class="form-group">
  188. <input type="submit" name="btnRegister" class="btn btn-primary" value="Register"/>
  189. </div>
  190. </form>
  191. </div>
  192. <div class="col-md-2"></div>
  193. <div class="col-md-5 well">
  194. <h4>Login</h4>
  195. <?php
  196. if ($login_error_message != "") {
  197. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $login_error_message . '</div>';
  198. }
  199. ?>
  200. <form action="index.php" method="post">
  201. <div class="form-group">
  202. <label for="">Username/Email</label>
  203. <input type="text" name="username" class="form-control"/>
  204. </div>
  205. <div class="form-group">
  206. <label for="">Password</label>
  207. <input type="password" name="password" class="form-control"/>
  208. </div>
  209. <div class="form-group">
  210. <input type="submit" name="btnLogin" class="btn btn-primary" value="Login"/>
  211. </div>
  212. </form>
  213. </div>
  214. </div>
  215. </div>
  216.  
  217. </body>
  218. </html>
  219.  
  220. <?php
  221.  
  222.  
  223. // Start Session
  224. session_start();
  225.  
  226. // Database connection
  227. require __DIR__ . '/database.php';
  228. $db = DB();
  229.  
  230. // Application library ( with DemoLib class )
  231. require __DIR__ . '/lib/library.php';
  232. $app = new DemoLib();
  233.  
  234. $login_error_message = '';
  235. $register_error_message = '';
  236.  
  237. // check Login request
  238. if (!empty($_POST['btnLogin'])) {
  239.  
  240. $username = trim($_POST['username']);
  241. $password = trim($_POST['password']);
  242.  
  243. if ($username == "") {
  244. $login_error_message = 'Username field is required!';
  245. } else if ($password == "") {
  246. $login_error_message = 'Password field is required!';
  247. } else {
  248. $user_id = $app->Login($username, $password); // check user login
  249. if($user_id > 0)
  250. {
  251. $_SESSION['user_id'] = $user_id; // Set Session
  252. header("Location: profile.php"); // Redirect user to the profile.php
  253. }
  254. else
  255. {
  256. $login_error_message = 'Invalid login details!';
  257. }
  258. }
  259. }
  260.  
  261. // check Register request
  262. if (!empty($_POST['btnRegister'])) {
  263. if ($_POST['name'] == "") {
  264. $register_error_message = 'Name field is required!';
  265. } else if ($_POST['email'] == "") {
  266. $register_error_message = 'Email field is required!';
  267. } else if ($_POST['username'] == "") {
  268. $register_error_message = 'Username field is required!';
  269. } else if ($_POST['password'] == "") {
  270. $register_error_message = 'Password field is required!';
  271. } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  272. $register_error_message = 'Invalid email address!';
  273. } else if ($app->isEmail($_POST['email'])) {
  274. $register_error_message = 'Email is already in use!';
  275. } else if ($app->isUsername($_POST['username'])) {
  276. $register_error_message = 'Username is already in use!';
  277. } else {
  278. $user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['username'], $_POST['password']);
  279. // set session and redirect user to the profile page
  280. $_SESSION['user_id'] = $user_id;
  281. header("Location: profile.php");
  282. }
  283. }
  284. ?>
  285. <!doctype html>
  286. <html lang="en">
  287. <head>
  288. <meta charset="UTF-8">
  289. <title>Home</title>
  290. <!-- Latest compiled and minified CSS -->
  291. <link rel="stylesheet" href="css/bootstrap.min.css">
  292. </head>
  293. <body>
  294.  
  295. <div class="container">
  296. <div class="row">
  297. <div class="col-md-12">
  298. <h2>
  299. link
  300. </h2>
  301. </div>
  302. </div>
  303. <div class="form-group">
  304. Note: welcome
  305. </div>
  306. <div class="row">
  307. <div class="col-md-5 well">
  308. <h4>Register</h4>
  309. <?php
  310. if ($register_error_message != "") {
  311. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $register_error_message . '</div>';
  312. }
  313. ?>
  314. <form action="index.php" method="post">
  315. <div class="form-group">
  316. <label for="">Name</label>
  317. <input type="text" name="name" class="form-control"/>
  318. </div>
  319. <div class="form-group">
  320. <label for="">Email</label>
  321. <input type="email" name="email" class="form-control"/>
  322. </div>
  323. <div class="form-group">
  324. <label for="">Username</label>
  325. <input type="text" name="username" class="form-control"/>
  326. </div>
  327. <div class="form-group">
  328. <label for="">Password</label>
  329. <input type="password" name="password" class="form-control"/>
  330. </div>
  331. <div class="form-group">
  332. <input type="submit" name="btnRegister" class="btn btn-primary" value="Register"/>
  333. </div>
  334. </form>
  335. </div>
  336. <div class="col-md-2"></div>
  337. <div class="col-md-5 well">
  338. <h4>Login</h4>
  339. <?php
  340. if ($login_error_message != "") {
  341. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $login_error_message . '</div>';
  342. }
  343. ?>
  344. <form action="index.php" method="post">
  345. <div class="form-group">
  346. <label for="">Username/Email</label>
  347. <input type="text" name="username" class="form-control"/>
  348. </div>
  349. <div class="form-group">
  350. <label for="">Password</label>
  351. <input type="password" name="password" class="form-control"/>
  352. </div>
  353. <div class="form-group">
  354. <input type="submit" name="btnLogin" class="btn btn-primary" value="Login"/>
  355. </div>
  356. </form>
  357. </div>
  358. </div>
  359. </div>
  360.  
  361. </body>
  362. </html>
  363.  
  364. <?php
  365. // Start Session
  366. session_start();
  367.  
  368. // check user login
  369. if(empty($_SESSION['user_id']))
  370. {
  371. header("Location: index.php");
  372. }
  373.  
  374. // Database connection
  375. require __DIR__ . '/database.php';
  376. $db = DB();
  377.  
  378. // Application library ( with DemoLib class )
  379. require __DIR__ . '/lib/library.php';
  380. $app = new DemoLib();
  381.  
  382. $user = $app->UserDetails($_SESSION['user_id']); // get user details
  383.  
  384. ?>
  385. <!doctype html>
  386. <html lang="en">
  387. <head>
  388. <meta charset="UTF-8">
  389. <title>Profile</title>
  390. <!-- Latest compiled and minified CSS -->
  391. <link rel="stylesheet" href="css/bootstrap.min.css">
  392. </head>
  393. <body>
  394. <div class="container">
  395. <div class="well">
  396. <h2>
  397. Profile
  398. </h2>
  399. <h3>Hello <?php echo $user->name ?>,</h3>
  400. <p>
  401. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur deserunt dolore fuga labore magni maxime, quaerat reiciendis tenetur? Accusantium blanditiis doloribus earum error inventore laudantium nesciunt quis reprehenderit ullam vel?
  402. </p>
  403. <a href="logout.php" class="btn btn-primary">Logout</a>
  404. </div>
  405. </div>
  406. </body>
  407. </html>
  408.  
  409. <?php
  410. /**
  411. * Tutorial: PHP Login Registration system
  412. *
  413. * Page : Logout
  414. */
  415.  
  416. // start session
  417. session_start();
  418.  
  419. // Destroy user session
  420. unset($_SESSION['user_id']);
  421.  
  422. // Redirect to index.php page
  423. header("Location: index.php");
  424. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement