register.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. require_once 'login.inc.php';
  3. if (isset($_POST['reg_username']) && isset($_POST['reg_password'])) {
  4. $userq->bindValue(':name', $_POST['reg_username']);
  5. $res = $userq->execute();
  6. if ($res === false || $res->fetchArray() === false) {
  7. $q = $db->prepare("INSERT INTO user (name, password) VALUES (:name, :password)");
  8. $q->bindValue(':name', $_POST['reg_username']);
  9. $q->bindValue(':password', password_hash($_POST['reg_password'], PASSWORD_DEFAULT));
  10. if ($q->execute()) {
  11. $res->finalize();
  12. $userq->bindValue(':name', $_POST['reg_username']);
  13. $res = $userq->execute();
  14. $row = $res->fetchArray();
  15. $_SESSION['cs4g_user_id'] = $row['id'];
  16. header('Location: ./');
  17. exit('Registration successful! <a href="./">Continue</a>');
  18. }
  19. else $login_error = "Hrm, something happened. Try again! (".$db->lastErrorMsg().")";
  20. } else {
  21. $login_error = "Someone has that username already...";
  22. }
  23. }
  24. include 'header.inc.php';
  25. ?>
  26. <h3>Register</h3>
  27. <p>User accounts are only used to track your progress through levels. Please note that Netsim is still in <strong>beta</strong>, so we may need to reset the user database from time to time.</p>
  28. <?=(isset($login_error) ? "<p>".$login_error."</p>\n" : "")?>
  29. <form method="post" action="register.php" onsubmit="if (document.getElementById('reg_password').value == document.getElementById('confirm_password').value) return true; else { alert('Passwords don\'t match!'); return false; }">
  30. <p>Username:<br>
  31. <input type="text" name="reg_username"></p>
  32. <p>Password:<br>
  33. <input type="password" name="reg_password" id="reg_password"></p>
  34. <p>Confirm password:<br>
  35. <input type="password" id="confirm_password"></p>
  36. <p><input type="submit" value="Register"></p>
  37. </form>
  38. <div style="height:150px;"></div>
  39. <script type="text/javascript">$('.reg_username').focus();</script>
  40. <?php include 'footer.inc.php'; ?>