PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/employers/login.php

https://bitbucket.org/jon0/jobislanddev
PHP | 340 lines | 285 code | 26 blank | 29 comment | 54 complexity | d91f77bb0377a63a1871afad23438d07 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 = ? AND type = 1");
  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. // check if the login form is submitted
  32. if (isset($_POST['submit']))
  33. {
  34. //Image Verification Start -- Check if string entered matches the md5 hash by the generated string
  35. $number = $_POST['number'];
  36. if (md5($number) != $_SESSION['image_random_value'])
  37. die ('Validation string not valid! Please try again! Click <a href=login.php>here</a> to go back.');
  38. else
  39. {
  40. // if form has been submitted
  41. // makes sure they filled it in
  42. if (!$_POST['username'] | !$_POST['password'])
  43. die('You did not fill in a required field.');
  44. // checks it against the database
  45. $statement = $db->prepare("SELECT * FROM account WHERE username= ? AND type = 1");
  46. $statement->execute(array($_POST['username']));
  47. //Gives error if user dosen't exist
  48. $check2 = $statement->rowCount();
  49. if ($check2 == 0)
  50. die('That user does not exist in our database. <a href=registration.php>Click Here to Register</a>');
  51. $info = $statement->fetch();
  52. $username = $_POST['username'];
  53. $pass = $_POST['password'];
  54. $password = sha1(strtoupper($username) . ":" . strtoupper($pass));
  55. //gives error if the password is wrong
  56. if ($password != $info['password'])
  57. die('Incorrect password, please try again. Click <a href="login.php">here</a> to go back.');
  58. else
  59. {
  60. // if login is ok then we add a cookie, allow it for all subfolders and update online status
  61. $hour = time() + 3600;
  62. setcookie('ID_my_site', $username, $hour, "/");
  63. setcookie('Key_my_site', $password, $hour, "/");
  64. $statement = $db->prepare("UPDATE account SET online = 1 WHERE username = ? AND type = 1");
  65. $statement->execute(array($username));
  66. //then redirect them to the main page
  67. header("Location: index.php");
  68. }
  69. }
  70. }
  71. // Reset Password
  72. if (empty($_GET['action']))
  73. $_GET['action'] = "";
  74. if (empty($_GET['reset']))
  75. $_GET['reset'] = 0;
  76. // Reset Form Start
  77. if (isset($_POST['submit']) && ($_GET['action'] == "resetpass"))
  78. {
  79. // if form has been submitted
  80. // makes sure they filled it in
  81. if(!$_POST['question'] | !$_POST['answer'] | !$_POST['username'])
  82. die('You did not fill in a required field.');
  83. // checks it against the database
  84. $check = $db->prepare("SELECT * FROM users u INNER JOIN account a WHERE u.account = a.id AND a.username= ? AND a.type = 1");
  85. $db->execute(array($_POST['username']));
  86. // Gives error if user dosen't exist
  87. $check2 = $check->rowcount();
  88. if ($check2 == 0)
  89. die
  90. ('
  91. That user does not exist in our database.
  92. <a href=registration.php>Click Here to Register</a> or
  93. <a href="login.php?action=resetpass> Click here to try again.</a>
  94. ');
  95. $info = $check->fetch();
  96. //gives error if the question/answer is wrong
  97. if ($_POST['question'] != $info['secQues'])
  98. die
  99. ('
  100. The question does not match the one in the database, please try again.
  101. Click <a href="login.php?action=resetpass">here</a> to go back.
  102. ');
  103. else if ($_POST['answer'] != $info['secAns'])
  104. die
  105. ('
  106. The answer does not match the one in the database, please try again.
  107. Click <a href="login.php?action=resetpass">here</a> to go back.
  108. ');
  109. else
  110. {
  111. $account = $info['id'];
  112. // if everything is ok then we allow to reset password via redirect to new page!
  113. header("Location: login.php?action=resetpass&reset=$account&cond=1");
  114. }
  115. }
  116. else if (isset($_POST['submit']) && ($_GET['action'] == "resetpass") && ($_GET['cond'] == 2))
  117. {
  118. // if form has been submitted
  119. // makes sure they filled it in
  120. if(!$_POST['pass'] | !$_POST['pass2'])
  121. die('You did not fill in a required field.');
  122. // this makes sure both passwords entered match
  123. if ($_POST['pass'] != $_POST['pass2'])
  124. die('Your passwords did not match. ');
  125. // here we encrypt the password and add slashes if needed
  126. if (!get_magic_quotes_gpc())
  127. $_POST['pass'] = addslashes($_POST['pass']);
  128. $sha_pass_hash = sha1(strtoupper($_POST['username']) . ":" . strtoupper($_POST['pass']));
  129. // now we insert it into the database
  130. $errors = "";
  131. if (!isset($_POST['pass']))
  132. $errors .= "Please provide a password in the first field. <br/>";
  133. if (!isset($_POST['pass2']))
  134. $errors .= "Please provide a password in the second field. <br/>";
  135. if ($errors == "")
  136. {
  137. $account = $_GET['reset'];
  138. $statement = $db->prepare("UPDATE account a INNER JOIN users u SET a.password= ? WHERE u.account = a.id AND u.account = AND a.id = ? AND a.username = ? AND a.type = 1");
  139. $statement->execute(array($sha_pass_hash, $account, $_POST['username']));
  140. $_GET['reset'] = 2;
  141. header("Location: login.php?action=resetpass&reset=2");
  142. }
  143. else
  144. echo $errors."Please go back and try again.";
  145. }
  146. // Reset Form End
  147. ?>
  148. <html>
  149. <head>
  150. <title>
  151. JobIsland for Businesses
  152. </title>
  153. <!-- CSS START -->
  154. <link rel="stylesheet" type="text/css" href="./../includes/invi.css" media="screen"/>
  155. <!-- CSS END -->
  156. <script language="javascript">
  157. <!-- redirect script -->
  158. function redirectPage()
  159. {
  160. document.location.href= "login.php"
  161. }
  162. </script>
  163. </head>
  164. <body>
  165. <!-- Left and Right Side Backgrounds -->
  166. <div class="leftbg">
  167. <div class="rightbg">
  168. <!-- Header Start -->
  169. <?php
  170. include "./../includes/head.php";
  171. ?>
  172. <!-- Header End -->
  173. <!-- Left Side Article Start -->
  174. <div id="left">
  175. <div class="left_articles">
  176. <?php
  177. if ($_GET['action'] == "resetpass")
  178. {
  179. echo '<center><b><p style="font-size: 16;"><a> Password Reset </a></p></b></center>';
  180. echo '<br><p> Please enter your username along with the secret question and your answer to it.';
  181. echo '<br> Once the question and answer has been verified you will be allowed to reset your password.</p>';
  182. echo '<form method="post" action="login.php?action=resetpass">';
  183. echo '<tr>
  184. <td>
  185. <p style="font-size: 14;">Your Username:</p>
  186. </td><td>
  187. <input type="text" name="username" maxlength="60" size="40"/>
  188. </td></tr><br><br>
  189. <tr><td>
  190. <p style="font-size: 14;">Your Secret Question:</p>
  191. </td><td>
  192. <input type="text" name="question" maxlength="60" size="40" />
  193. </td></tr><br><br>
  194. <tr><td>
  195. <p style="font-size: 14;">Your Secret Answer:</p>
  196. </td><td>
  197. <input type="text" name="answer" maxlength="60" size="40" />
  198. </td></tr><br><br>
  199. <tr><th colspan=2>
  200. <input type="submit" name="submit" value="Reset Password" />
  201. <input type="button" value="Back" onClick="redirectPage()">
  202. </th></tr>
  203. </table>
  204. </form><br>
  205. ';
  206. }
  207. else if (($_GET['action'] == "resetpass") && ($_GET['reset'] > 0) && ($_GET['cond'] == 1))
  208. {
  209. $user = $_GET['reset'];
  210. echo '<center><b><p style="font-size: 16;"><a> Password Reset </a></p></b></center>';
  211. echo '<p> Please enter your new password. </p>';
  212. echo '<p> The current password will now be overwritten with the new one.</p><br>';
  213. echo '<form method="post" action="login.php?action=resetpass&reset='.$user.'&cond=2">';
  214. echo '<tr>
  215. <td>
  216. <p style="font-size: 14;">Your New Password:</p>
  217. </td>
  218. <td>
  219. <input type="password" name="pass" maxlength="60" size="40" />
  220. </td>
  221. </tr>
  222. <br><br>
  223. <tr>
  224. <td>
  225. <p style="font-size: 14;">Your New Password Again:</p>
  226. </td>
  227. <td>
  228. <input type="password" name="pass2" maxlength="60" size="40" />
  229. </td>
  230. </tr>
  231. <br><br>
  232. <tr><th colspan=2>
  233. <input type="submit" name="submit" value="Reset Password">
  234. </th></tr>
  235. </table>
  236. </form>
  237. ';
  238. }
  239. else if (($_GET['action'] == "resetpass") && ($_GET['reset'] == 2) && ($_GET['cond'] == 3))
  240. {
  241. echo '<center><b><p style="font-size: 16;"><a> Password Reset </a></p></b></center>';
  242. echo '<p>Your password has been successfully changed!</p>';
  243. echo '<p>You may now log in using your new password</p>';
  244. echo '<p> Click <a href="login.php">here</a> to login now!</p></center>';
  245. header("Location: logout.php");
  246. }
  247. else
  248. {
  249. ?>
  250. <b><p style="font-size: medium" align="center">Business Parter, you are? Not? Register <a href="registration.php">here</a>, you may.</p></b>
  251. <!-- 2 ways to pass form, using PHP_SELF or just putting in the location and file name. Both works fine. -->
  252. <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
  253. <table border="0" align="center">
  254. <tr>
  255. <td>Username: </td>
  256. <td><input type="text" name="username" maxlength="40"></td>
  257. </tr>
  258. <tr>
  259. <td>Password: </td>
  260. <td><input type="password" name="password" maxlength="50"></td>
  261. </tr>
  262. <tr>
  263. <td>Image Verification:</td>
  264. <td><input name='number' type="text" id=\'number\''></td>
  265. </tr>
  266. <tr>
  267. <td colspan="2" align="center"><img alt='' src='./../includes/random_image.php' /></td>
  268. </tr>
  269. </table>
  270. <p style="font-size: small;" align="center">
  271. Please enter the string shown in the image above.
  272. </p>
  273. <br>
  274. <center>
  275. <tr><td colspan="2" align="center">
  276. <input type="submit" name="submit" value="Login">
  277. </td></tr>
  278. </center>
  279. </form>
  280. <p style="font-size: small;" align="center">Forgot your password? Click <b><a href="login.php?action=resetpass">here to retrieve it!</a></b>
  281. <br/><br/>
  282. <p style="font-weight: bold; font-size: 14;" align="center">Came here by mistake? Click <b><a href="./../login.php">here to go back to the home page!</a></b>
  283. <?php
  284. }
  285. ?>
  286. </div>
  287. </div>
  288. <!-- Left Side Article End -->
  289. <!-- Right Side Article Start -->
  290. <div id="right">
  291. <br/>
  292. <center>
  293. <img src="http://static.tumblr.com/poxjlqi/qCCl52mnu/placeholder_ad.png">
  294. </center>
  295. </div>
  296. <!-- Right Side Article End -->
  297. <!-- Footer Start -->
  298. <?php
  299. include './../includes/footer.php';
  300. ?>
  301. <!-- Footer End -->
  302. </div>
  303. </div>
  304. </body>
  305. </html>