PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://gitlab.com/bkrpage/php-Login-System
PHP | 167 lines | 126 code | 39 blank | 2 comment | 27 complexity | 503bdbf261035513b9f38a9b75fcadc8 MD5 | raw file
  1. <?php
  2. session_start();
  3. $page_title = "Home ";
  4. require 'header.php';
  5. if (isset($_COOKIE['user'])){
  6. $_SESSION['loggedin'] = true;
  7. $_SESSION['userID'] = $_COOKIE['user'];
  8. }
  9. if (isset($_SESSION['loggedin'])){
  10. if ($_SESSION['loggedin'] == true){
  11. header('Location: control_panel.php');
  12. } else {
  13. echo "Error";
  14. unset($_SESSION['loggedin']);
  15. }
  16. } else {
  17. ?>
  18. <div class="form-box index">
  19. <h1> Login </h1>
  20. <p class="notification"> Currently, due to my hostings Database restrictions, the website is not functioning on bkrpage.co.uk right now. However,
  21. there is a working version at <a href="http://student20352.201415.uk">my University sub-domain</a>.</p>
  22. <?php
  23. if ($_GET['successfulReset']){
  24. unset($_SESSION['user_resetting_pass']);
  25. echo "<p class='success'>Password successfully reset, now you can login below</p>";
  26. }
  27. if ($_GET['registered']){
  28. echo "<p class='success'>Successfully registered. Please login below</p>";
  29. }
  30. if ($_GET['loggedout']){
  31. echo "<p class='success'>You have logged out. Goodbye!</p>";
  32. }
  33. if ($_GET['alreadyloggedout']){
  34. echo "<p class='warning'>You are already logged out</p>";
  35. }
  36. if ($_GET['notLoggedIn']){
  37. echo "<p class='warning'>You are not logged in</p>";
  38. }
  39. if (!empty($_POST)){
  40. $email = $_POST['email'];
  41. $password = $_POST['password'];
  42. $hashed_pw = SHA1("$password");
  43. $remember_me = $_POST['remember'];
  44. $entry_errors = array();
  45. if (empty($email)){
  46. $entry_errors[] = "<p class='error'>Please enter your Email</p><style>.e{border: 1px solid #CC0000;}</style>";
  47. } else {
  48. $uid = "i7709331";
  49. $pwd = "phppass";
  50. $host = "127.0.0.1";
  51. $db = $uid;
  52. $conn = mysqli_connect($host, $uid, $pwd, $db);
  53. $q_email_check = "SELECT u_email FROM users WHERE u_email LIKE '$email'"; //Query to find duplicate emails
  54. $result_email = mysqli_query($conn, $q_email_check);
  55. $num_rows = mysqli_fetch_array($result_email);
  56. if (empty($num_rows)){
  57. $entry_errors[] = "<p class='error'>User does not exist</p><style>.e{border: 1px solid #CC0000;}</style>";
  58. }
  59. }
  60. if (empty($password) && empty($entry_errors)){
  61. $entry_errors[] = "<p class='error'>Please enter a Password</p><style>.pw{border: 1px solid #CC0000;}</style>";
  62. } else if (empty($entry_errors)){
  63. $uid = "i7709331";
  64. $pwd = "phppass";
  65. $host = "127.0.0.1";
  66. $db = $uid;
  67. $conn = mysqli_connect($host, $uid, $pwd, $db);
  68. $q_password_check = "SELECT u_password FROM users WHERE u_email LIKE '$email' AND u_password LIKE '$hashed_pw'"; //Query to find duplicate emails
  69. $result_password = mysqli_query($conn, $q_password_check);
  70. $num_rows = mysqli_fetch_array($result_password);
  71. if (empty($num_rows)){
  72. $entry_errors[] = "<p class='error'>Password is incorrect</p><style>.pw{border: 1px solid #CC0000;}</style>";
  73. }
  74. }
  75. if(empty($entry_errors)){
  76. $uid = "i7709331";
  77. $pwd = "phppass";
  78. $host = "127.0.0.1";
  79. $db = $uid;
  80. $conn = mysqli_connect($host, $uid, $pwd, $db);
  81. //escapes any mysqli commands
  82. $email = mysqli_real_escape_string($conn, $email);
  83. $qry = "SELECT * FROM users WHERE u_email LIKE '$email' AND u_password LIKE '$hashed_pw'";
  84. $result = mysqli_query($conn,$qry);
  85. $rows = mysqli_num_rows($result);
  86. if ($rows == 1){
  87. $_SESSION['loggedin'] = true;
  88. $_SESSION['userID'] = $email;
  89. //set cookie to stay logged in if wanted
  90. if ($remember_me == "true"){
  91. $cookie_name = "user";
  92. $cookie_value = $email;
  93. $cookie_time = time() + 3600 * 24 * 7; //setting cookie expiry time for a week
  94. setcookie($cookie_name, $cookie_value, $cookie_time);
  95. }
  96. header('Location: control_panel.php');
  97. }
  98. mysqli_close($conn);
  99. } else {
  100. foreach($entry_errors as $e){
  101. echo "$e";
  102. }
  103. }
  104. }
  105. ?>
  106. <form action="index.php" method="POST">
  107. <label for="email">Email</label>
  108. <input type="email" name="email" value="<?php if(!empty($email)) echo "$email" ; ?>" class="e">
  109. <label for="password">Password</label>
  110. <input type="password" name="password" class="pw">
  111. <label id="rememberme"><input type="checkbox" name="remember" value="true">Remember me</label>
  112. <input type="submit" value="Login" class="submit login">
  113. </form>
  114. <form action="register.php">
  115. <input type="submit" value="No account? Register!" class="submit register">
  116. </form>
  117. <form action="forgot_password.php">
  118. <input type="submit" value="Forgot Password?" class="submit forgot">
  119. </form>
  120. </div>
  121. <?php
  122. }
  123. ?>
  124. <footer>
  125. <div id="github"> This site was made by Bradley Page using PHP and MySQLi. To see the working innards as well as my other projects, check out my <a href="https://github.com/bkrpage/php-Login-System">Github Repo</a>! </div>
  126. </footer>
  127. </body>
  128. </html>