PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/login.php

https://bitbucket.org/jon0/jobislanddev
PHP | 362 lines | 303 code | 29 blank | 30 comment | 63 complexity | 3541bb4aa4402cb426afbdef23593174 MD5 | raw file
  1. <?php
  2. session_start();
  3. // Include the config file which connects to database and bbcode array
  4. include "./includes/config.php";
  5. // Checks if there is an existing login cookie
  6. // if cookie does not exist, continue
  7. // this is specifically needed here because we will be creating a cookie here and also to avoid a possible infinite loop
  8. if (isset($_COOKIE['ID_my_site']))
  9. {
  10. $username = $_COOKIE['ID_my_site'];
  11. $password = $_COOKIE['Key_my_site'];
  12. $statement = $db->prepare("SELECT * FROM account WHERE username = ?");
  13. $statement->execute(array($username));
  14. $info = $statement->fetch();
  15. // if cookie has wrong stored password, expire it and take them back to login page
  16. if ($password != $info['password'])
  17. {
  18. $past = time() - 3600;
  19. //destroy the cookie
  20. setcookie('ID_my_site', '', $past, "/");
  21. setcookie('Key_my_site', '', $past, "/");
  22. unset($_COOKIE['ID_my_site']);
  23. unset($_COOKIE['Key_my_site']);
  24. session_destroy();
  25. }
  26. else
  27. ("Location: index.php");
  28. }
  29. else
  30. ("Location: index.php");
  31. // Reset Password
  32. if (empty($_GET['action']))
  33. $_GET['action'] = "";
  34. if (empty($_GET['reset']))
  35. $_GET['reset'] = 0;
  36. // Reset Form Start
  37. if (isset($_POST['submit']) && ($_GET['action'] == "resetpass") && $_POST['question'] && $_POST['answer'] && $_POST['username'])
  38. {
  39. // if form has been submitted
  40. // makes sure they filled it in
  41. if(!$_POST['question'] || !$_POST['answer'] || !$_POST['username'])
  42. die('You did not fill in a required field.');
  43. // checks it against the database
  44. $check = $db->prepare("SELECT * FROM users u INNER JOIN account a WHERE u.account = a.id AND a.username= ?");
  45. $check->execute(array($_POST['username']));
  46. // Gives error if user dosen't exist
  47. $check2 = $check->rowcount();
  48. if ($check2 == 0)
  49. die
  50. ('
  51. That user does not exist in our database.
  52. <a href=registration.php>Click Here to Register</a> or
  53. <a href="login.php?action=resetpass> Click here to try again.</a>
  54. ');
  55. $info = $check->fetch();
  56. $_POST['question'] = addslashes($_POST['question']);
  57. $_POST['answer'] = addslashes($_POST['answer']);
  58. //gives error if the question/answer is wrong
  59. if ($_POST['question'] != $info['secQues'])
  60. die
  61. ('
  62. The question does not match the one in the database, please try again.
  63. Click <a href="login.php?action=resetpass">here</a> to go back.
  64. ');
  65. else if ($_POST['answer'] != $info['secAns'])
  66. {
  67. die
  68. ('
  69. The answer does not match the one in the database, please try again.
  70. Click <a href="login.php?action=resetpass">here</a> to go back.
  71. ');
  72. }
  73. else
  74. {
  75. $account = $info['id'];
  76. // if everything is ok then we allow to reset password via redirect to new page!
  77. header("Location: login.php?action=resetpass&reset=$account&cond=1");
  78. }
  79. }
  80. else if (isset($_POST['submit']) && ($_GET['action'] == "resetpass") && ($_GET['cond'] == 2))
  81. {
  82. // if form has been submitted
  83. // makes sure they filled it in
  84. if(!$_POST['pass'] | !$_POST['pass2'])
  85. die('You did not fill in a required field.');
  86. // this makes sure both passwords entered match
  87. if ($_POST['pass'] != $_POST['pass2'])
  88. die('Your passwords did not match. ');
  89. // here we encrypt the password and add slashes if needed
  90. if (!get_magic_quotes_gpc())
  91. $_POST['pass'] = addslashes($_POST['pass']);
  92. $sha_pass_hash = sha1(strtoupper($_POST['username']) . ":" . strtoupper($_POST['pass']));
  93. // now we insert it into the database
  94. $errors = "";
  95. if (!isset($_POST['pass']))
  96. $errors .= "Please provide a password in the first field. <br/>";
  97. if (!isset($_POST['pass2']))
  98. $errors .= "Please provide a password in the second field. <br/>";
  99. if ($errors == "")
  100. {
  101. $account = $_GET['reset'];
  102. $statement = $db->prepare("UPDATE account a INNER JOIN users u SET a.password= ?, dateReg=NOW() WHERE u.account = a.id AND u.account = AND a.id = ? AND a.username = ?");
  103. $statement->execute(array($sha_pass_hash, $account, $_POST['username']));
  104. $_GET['reset'] = 2;
  105. header("Location: login.php?action=resetpass&reset=2");
  106. }
  107. else
  108. echo $errors."Please go back and try again.";
  109. }
  110. // Reset Form End
  111. // check if the login form is submitted
  112. else if (isset($_POST['submit']))
  113. {
  114. //Image Verification Start -- Check if string entered matches the md5 hash by the generated string
  115. $number = $_POST['number'];
  116. if (md5($number) != $_SESSION['image_random_value'])
  117. die ('Validation string not valid! Please try again! Click <a href=login.php>here</a> to go back.');
  118. else
  119. {
  120. // if form has been submitted
  121. // makes sure they filled it in
  122. if (!$_POST['username'] | !$_POST['password'])
  123. die('You did not fill in a required field.');
  124. // checks it against the database
  125. $statement = $db->prepare("SELECT * FROM account WHERE username= ?");
  126. $statement->execute(array($_POST['username']));
  127. //Gives error if user dosen't exist
  128. $check2 = $statement->rowCount();
  129. if ($check2 == 0)
  130. die('That user does not exist in our database. <a href=registration.php>Click Here to Register</a>');
  131. $info = $statement->fetch();
  132. $username = $_POST['username'];
  133. $pass = $_POST['password'];
  134. $password = sha1(strtoupper($username) . ":" . strtoupper($pass));
  135. //gives error if the password is wrong
  136. if ($password != $info['password'])
  137. die('Incorrect password, please try again. Click <a href="login.php">here</a> to go back.');
  138. else
  139. {
  140. // if login is ok then we add a cookie, allow it for all subfolders and update online status
  141. $hour = time() + 3600;
  142. setcookie('ID_my_site', $username, $hour, "/");
  143. setcookie('Key_my_site', $password, $hour, "/");
  144. $statement = $db->prepare("UPDATE account SET online = 1 WHERE username = ? AND type = 0");
  145. $statement->execute(array($username));
  146. $check = $db->prepare("SELECT * FROM account WHERE username= ?");
  147. $check->execute(array($username));
  148. $info = $check->fetch();
  149. if ($info['type'] == 0 || 3)
  150. //then redirect them to the main page
  151. header("Location: index.php");
  152. if ($info['type'] == 1)
  153. header("Location: ./employers/index.php");
  154. }
  155. }
  156. }
  157. ?>
  158. <html>
  159. <head>
  160. <title>
  161. Log In to JobIsland
  162. </title>
  163. <!-- CSS START -->
  164. <link rel="stylesheet" type="text/css" href="./includes/invi.css" media="screen"/>
  165. <!-- CSS END -->
  166. <script language="javascript">
  167. <!-- redirect script -->
  168. function redirectPage()
  169. {
  170. document.location.href= "login.php"
  171. }
  172. </script>
  173. </head>
  174. <body>
  175. <!-- Left and Right Side Backgrounds -->
  176. <div class="leftbg">
  177. <div class="rightbg">
  178. <!-- Header Start -->
  179. <?php
  180. include "./includes/head.php";
  181. ?>
  182. <!-- Header End -->
  183. <!-- Left Side Article Start -->
  184. <div id="left">
  185. <div class="left_articles">
  186. <?php
  187. if ($_GET['action'] == "resetpass")
  188. {
  189. echo '<center><b><p style="font-size: 16;"><a> Password Reset </a></p></b></center>';
  190. echo '<br><p> Please enter your username along with the secret question and your answer to it.';
  191. echo '<br> Once the question and answer has been verified you will be allowed to reset your password.</p>';
  192. echo '<form method="post" action="login.php?action=resetpass">';
  193. echo '<tr>
  194. <td>
  195. <p style="font-size: 14;">Your Username:</p>
  196. </td><td>
  197. <input type="text" name="username" maxlength="60" size="40"/>
  198. </td></tr><br><br>
  199. <tr><td>
  200. <p style="font-size: 14;">Your Secret Question:</p>
  201. </td><td>
  202. <input type="text" name="question" maxlength="60" size="40" />
  203. </td></tr><br><br>
  204. <tr><td>
  205. <p style="font-size: 14;">Your Secret Answer:</p>
  206. </td><td>
  207. <input type="text" name="answer" maxlength="60" size="40" />
  208. </td></tr><br><br>
  209. <tr><th colspan=2>
  210. <input type="submit" name="submit" value="Reset Password" />
  211. <input type="button" value="Back" onClick="redirectPage()">
  212. </th></tr>
  213. </table>
  214. </form><br>
  215. ';
  216. }
  217. if (($_GET['action'] == "resetpass") && ($_GET['reset'] == 1) && ($_GET['cond'] == 1))
  218. {
  219. $user = $_GET['reset'];
  220. echo '<center><b><p style="font-size: 16;"><a> Password Reset </a></p></b></center>';
  221. echo '<p> Please enter your new password. </p>';
  222. echo '<p> The current password will now be overwritten with the new one.</p><br>';
  223. echo '<form method="post" action="login.php?action=resetpass&reset='.$user.'&cond=2">';
  224. echo '<tr>
  225. <td>
  226. <p style="font-size: 14;">Your New Password:</p>
  227. </td>
  228. <td>
  229. <input type="password" name="pass" maxlength="60" size="40" />
  230. </td>
  231. </tr>
  232. <br><br>
  233. <tr>
  234. <td>
  235. <p style="font-size: 14;">Your New Password Again:</p>
  236. </td>
  237. <td>
  238. <input type="password" name="pass2" maxlength="60" size="40" />
  239. </td>
  240. </tr>
  241. <br><br>
  242. <tr><th colspan=2>
  243. <input type="submit" name="submit" value="Reset Password">
  244. </th></tr>
  245. </table>
  246. </form>
  247. ';
  248. }
  249. if (($_GET['action'] == "resetpass") && ($_GET['reset'] == 2) && ($_GET['cond'] == 3))
  250. {
  251. echo '<center><b><p style="font-size: 16;"><a> Password Reset </a></p></b></center>';
  252. echo '<p>Your password has been successfully changed!</p>';
  253. echo '<p>You may now log in using your new password</p>';
  254. echo '<p> Click <a href="login.php">here</a> to login now!</p></center>';
  255. header("Location: logout.php");
  256. }
  257. else
  258. {
  259. echo '
  260. <b><p style="font-size: medium">So, we\'ve heard you\'re looking for a job?</p></b>
  261. <br>
  262. <p style="font-size: small;">
  263. To get to experience the sunny beaches of Job Island you first need to log in. We don\'t want sharks in our waters so we insist on it.
  264. <br>
  265. We\'re sorry but it is needed. Also cookies? They need to be enabled as well.
  266. <br><br>
  267. What? You don\'t have an account? Then hurry up and register <b><a href="registration.php">here!</a></b>
  268. <br><br></p><br>
  269. <b><p style="font-size: medium">Are you an employer? Don\'t have an account yet? Register <a href="./employers/registration.php">here!</a></p></b>
  270. <p style="font-size: small;">
  271. Want to affiliate your company with our service? Head on over to the <b><a href="faq.php">FAQ section</a></b> to see how.
  272. </p>';
  273. }
  274. // Reset Pass End
  275. ?>
  276. </div>
  277. </div>
  278. <!-- Left Side Article End -->
  279. <!-- Right Side Article Start -->
  280. <div id="right">
  281. <div class="right_articles">
  282. <b><p style="font-size: medium" align="center">Log In</p></b>
  283. <!-- 2 ways to pass form, using PHP_SELF or just putting in the location and file name. Both works fine. -->
  284. <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
  285. <table border="0" align="center">
  286. <tr>
  287. <td>Username: </td>
  288. <td><input type="text" name="username" maxlength="40"></td>
  289. </tr>
  290. <tr>
  291. <td>Password: </td>
  292. <td><input type="password" name="password" maxlength="50"></td>
  293. </tr>
  294. <tr>
  295. <td>Image Verification:</td>
  296. <td><input name='number' type="text" id=\'number\''></td>
  297. </tr>
  298. <tr>
  299. <td colspan="2" align="center"><img alt='' src='./includes/random_image.php' /></td>
  300. </tr>
  301. </table>
  302. <p style="font-size: small;" align="center">
  303. Please enter the string shown in the image above.
  304. </p>
  305. <br>
  306. <center>
  307. <tr><td colspan="2" align="center">
  308. <input type="submit" name="submit" value="Login">
  309. </td></tr>
  310. </center>
  311. </form>
  312. <p style="font-size: small;" align="center">Forgot your password? Click <b><a href="login.php?action=resetpass">here to retrieve it!</a></b>
  313. </div>
  314. </div>
  315. <!-- Right Side Article End -->
  316. <!-- Footer Start -->
  317. <?php
  318. include './includes/footer.php';
  319. ?>
  320. <!-- Footer End -->
  321. </div>
  322. </div>
  323. </body>
  324. </html>