Guest User

Untitled

a guest
Apr 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. <?php
  2. session_start();
  3. // Required files
  4. require_once '../php/logOut.php';
  5. require_once '../php/redirections.php';
  6. require_once '../php/passwordGenerator.php';
  7. require_once '../php/printDataProcess.php';
  8. // Checking the session
  9. if(!isset($_SESSION['userSession'])){
  10. header("Location: ../index.php");
  11. }
  12. // Checking the session
  13. if (!isset($_SESSION['created'])) {
  14. $_SESSION['created'] = time();
  15. } else if (time() - $_SESSION['created'] > 3600) {
  16. // session started more than 1 hour ago
  17. session_regenerate_id(true); // change session ID for the current session and invalidate old session ID
  18. $_SESSION['created'] = time(); // update creation time
  19. logOut();
  20. }
  21.  
  22. if (isset($_GET['logOut'])) {
  23. logOut();
  24. }
  25. ?>
  26. <!DOCTYPE html>
  27. <html lang="en">
  28. <head>
  29. <meta charset="UTF-8">
  30. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  31. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  32. <meta charset="utf-8">
  33. <!-- CSS imports -->
  34. <link rel="stylesheet" href="../css/main.css" />
  35. <!-- JQuery include -->
  36. <script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
  37. <script type="text/javascript" src="../js/registerUser.js"></script>
  38. <!-- <script type="text/javascript" src="../js/checkMail.js"></script> -->
  39. <title>Create user</title>
  40. </head>
  41. <body>
  42. <!-- Navigation Bar content -->
  43. <?php
  44. $userListHandler = PrintDataHandler::getInstance();
  45. $userListHandler->printNavBar();
  46. ?>
  47. <!-- Body content -->
  48. <div class="bodyContainer">
  49. <?php
  50. echo "<div id='userGreet'>
  51. <h3>Welcome, ".$_SESSION['userSession']['name']." ".$_SESSION['userSession']['surname']."</h3>
  52. </div>";
  53. ?>
  54. <div class="formContainer">
  55. <p class="formTitle">Create a new user</p>
  56. <form id="registerForm" method="post">
  57. <div class="formDataContainer">
  58. <div id="error"><!-- Error will be shown here ! --></div>
  59. <label for="userName"><span class="labelText">Name</span></label>
  60. <input type="text" placeholder="Name" name="userName" minlength="2" maxlength="100" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" required>
  61. <label for="userSurname"><span class="labelText">Surname</span></label>
  62. <input type="text" placeholder="Surname" name="userSurname" minlength="2" maxlength="100" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" required>
  63. <label for="userMail"><span class="labelText">Email</span></label>
  64. <input type="email" placeholder="Enter email" id="userMail" name="userMail" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" required>
  65. <label for="psw"><span class="labelText">Password</span></label>
  66. <input type="password" placeholder="Enter Password" name="psw" required autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" required>
  67. <label for="pswRepeat"><span class="labelText">Repeat your password</span></label>
  68. <input type="password" placeholder="Enter Password" name="pswRepeat" required autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" required>
  69. <button id="btnCancel" name="btnCancel" onclick="location.href = 'userPanel.php';">Cancel</button>
  70. <button type="submit" id="btnRegister" name="btnRegister">Register the user</button>
  71. </div>
  72. </form>
  73. </div>
  74. </div>
  75. </body>
  76. </html>
  77.  
  78. $(document).ready(function(){
  79. $('#error').fadeOut();
  80. $('form[id='registerForm']').on('submit', function(e){
  81. e.preventDefault();
  82. $("#error").fadeOut();
  83. var data = $(this).serialize();
  84. $.ajax({
  85. type: 'POST', url: '../php/registerProcess.php', data: data, beforeSend: function(){
  86. $('#error').fadeOut();
  87. $('#btnRegister').html('Sending ...');
  88. }, success: function(response){
  89. if (response == 'ok'){
  90. $('#btnRegister').html('<img src="../resources/icons/ajax-loader.gif" /> &nbsp; Creating the user ...');
  91. setTimeout(' window.location.href = "userCreated.html"; ', 3000);
  92. }else{
  93. $('#error').fadeIn(1000, function(){
  94. $('#error').html(response);
  95. $('#btnRegister').html('Register the user');
  96. });
  97. }
  98. },
  99. });
  100. return false;
  101. });
  102. });
  103.  
  104. Que a su vez ejecuta el script de php siguiente:
  105.  
  106. <?php
  107. require_once 'dbConfig.php';
  108. // Clean obtained data
  109. $userMail = $mysqli->real_escape_string(trim($_POST["userMail"]));
  110. $passwd = $mysqli->real_escape_string(trim($_POST["psw"]));
  111. $userSurname = $mysqli->real_escape_string(trim($_POST["userSurname"]));
  112. $userName = $mysqli->real_escape_string(trim($_POST["userName"]));
  113. $level = 0;
  114. // Crypt the Password
  115. $password = md5($passwd);
  116. // Starting a transaction
  117. try{
  118. $mysqli->begin_transaction();
  119. // Attempt to prepare the query
  120. if($mysqli->query("INSERT INTO users (idUser,name,surname,mail,password,level)
  121. VALUES (null,'$userName','$userSurname','$userMail','$password','$level')") === TRUE){
  122. $mysqli->commit();
  123. echo "ok";
  124. }
  125. }catch(Exception $e){
  126. $msli->rollback();
  127. echo $e;
  128. }
  129. $mysqli->close();
  130. ?>
Add Comment
Please, Sign In to add comment