Advertisement
sallymcsalad

Untitled

Feb 28th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // Application library ( with DemoLib class )
  5. require __DIR__ . '/ajax/lib.php';
  6. $app = new DemoLib();
  7. $db = DB();
  8.  
  9. $login_error_message = '';
  10. $register_error_message = '';
  11.  
  12. // check Login request
  13. if (!empty($_POST['btnLogin'])) {
  14.  
  15. $username = trim($_POST['username']);
  16. $password = trim($_POST['password']);
  17.  
  18. if ($username == "") {
  19. $login_error_message = 'Username field is required!';
  20. } else if ($password == "") {
  21. $login_error_message = 'Password field is required!';
  22. } else {
  23. $user_id = $app->Login($username, $password); // check user login
  24. if($user_id > 0)
  25. {
  26. $_SESSION['user_id'] = $user_id; // Set Session
  27. header("Location: index3.php"); // Redirect user to the profile.php
  28. }
  29. else
  30. {
  31. $login_error_message = 'Invalid login details!';
  32. }
  33. }
  34. }
  35.  
  36. // check Register request
  37. if (!empty($_POST['btnRegister'])) {
  38. if ($_POST['name'] == "") {
  39. $register_error_message = 'Name field is required!';
  40. } else if ($_POST['email'] == "") {
  41. $register_error_message = 'Email field is required!';
  42. } else if ($_POST['username'] == "") {
  43. $register_error_message = 'Username field is required!';
  44. } else if ($_POST['password'] == "") {
  45. $register_error_message = 'Password field is required!';
  46. } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  47. $register_error_message = 'Invalid email address!';
  48. } else if ($app->isEmail($_POST['email'])) {
  49. $register_error_message = 'Email is already in use!';
  50. } else if ($app->isUsername($_POST['username'])) {
  51. $register_error_message = 'Username is already in use!';
  52. } else {
  53. $user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['username'], $_POST['password']);
  54. // set session and redirect user to the profile page
  55. $_SESSION['user_id'] = $user_id;
  56. header("Location: index3.php");
  57. }
  58. }
  59. ?>
  60. <!doctype html>
  61. <html lang="en">
  62. <head>
  63. <meta charset="UTF-8">
  64. <title>Home</title>
  65. <!-- Latest compiled and minified CSS -->
  66. <link rel="stylesheet" href="css/bootstrap.min.css">
  67. <link rel="stylesheet" href="css/loginforuser.css">
  68. </head>
  69. <body>
  70. <div class="container">
  71. <div class="row">
  72. <div class="col-md-5 card mt-4">
  73. <h4 class="pt-3 pb-3">Register</h4>
  74. <?php
  75. if ($register_error_message != "") {
  76. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $register_error_message . '</div>';
  77. }
  78. ?>
  79. <form action="loginforuser.php" method="post">
  80. <div class="form-group">
  81. <label for="">Name</label>
  82. <input type="text" name="name" class="form-control"/>
  83. </div>
  84. <div class="form-group">
  85. <label for="">Email</label>
  86. <input type="email" name="email" class="form-control"/>
  87. </div>
  88. <div class="form-group">
  89. <label for="">Username</label>
  90. <input type="text" name="username" class="form-control"/>
  91. </div>
  92. <div class="form-group">
  93. <label for="">Password</label>
  94. <input type="password" name="password" class="form-control"/>
  95. </div>
  96. <div class="form-group">
  97. <input type="submit" name="btnRegister" class="btn btn-primary" value="Register"/>
  98. </div>
  99. </form>
  100. </div>
  101. <div class="col-md-2"></div>
  102. <div class="col-md-5 card mt-4">
  103. <h4 class="pt-3 pb-3">Login</h4>
  104. <?php
  105. if ($login_error_message != "") {
  106. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $login_error_message . '</div>';
  107. }
  108. ?>
  109. <form action="loginforuser.php" method="post">
  110. <div class="form-group">
  111. <label for="">Username/Email</label>
  112. <input type="text" name="username" class="form-control"/>
  113. </div>
  114. <div class="form-group">
  115. <label for="">Password</label>
  116. <input type="password" name="password" class="form-control"/>
  117. </div>
  118. <div class="form-group">
  119. <input type="submit" name="btnLogin" class="btn btn-primary" value="Login"/>
  120. </div>
  121. </form>
  122. </div>
  123. </div>
  124. </div>
  125.  
  126. </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement