Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. <?php require('includes/config.php');
  2.  
  3. //if logged in redirect to members page
  4. if( $user->is_logged_in() ){ header('Location: memberpage.php'); }
  5.  
  6. //if form has been submitted process it
  7. if(isset($_POST['submit'])){
  8.  
  9. //very basic validation
  10. if(strlen($_POST['username']) < 3){
  11. $error[] = 'Username is too short.';
  12. } else {
  13. $stmt = $db->prepare('SELECT username FROM members WHERE username = :username');
  14. $stmt->execute(array(':username' => $_POST['username']));
  15. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  16.  
  17. if(!empty($row['username'])){
  18. $error[] = 'Username provided is already in use.';
  19. }
  20.  
  21. }
  22.  
  23. if(strlen($_POST['password']) < 3){
  24. $error[] = 'Heslo musí obsahovat 3+ znaků!';
  25. }
  26.  
  27. if(strlen($_POST['passwordConfirm']) < 3){
  28. $error[] = 'Heslo musí obsahovat 3+ znaků!';
  29. }
  30.  
  31. if($_POST['password'] != $_POST['passwordConfirm']){
  32. $error[] = 'Hesla se neschodují!';
  33. }
  34.  
  35. //email validation
  36. if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
  37. $error[] = 'Prosím zadej platný email.';
  38. } else {
  39. $stmt = $db->prepare('SELECT email FROM members WHERE email = :email');
  40. $stmt->execute(array(':email' => $_POST['email']));
  41. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  42.  
  43. if(!empty($row['email'])){
  44. $error[] = 'Tento email již byl použit!';
  45. }
  46.  
  47. }
  48.  
  49.  
  50. //if no errors have been created carry on
  51. if(!isset($error)){
  52.  
  53. //hash the password
  54. $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);
  55.  
  56. //create the activasion code
  57. $activasion = md5(uniqid(rand(),true));
  58.  
  59. try {
  60.  
  61. //insert into database with a prepared statement
  62. $stmt = $db->prepare('INSERT INTO members (username,password,email,active) VALUES (:username, :password, :email, :active)');
  63. $stmt->execute(array(
  64. ':username' => $_POST['username'],
  65. ':password' => $hashedpassword,
  66. ':email' => $_POST['email'],
  67. ':active' => $activasion
  68. ));
  69. $id = $db->lastInsertId('memberID');
  70.  
  71. //send email
  72. $to = $_POST['email'];
  73. $subject = "Registrace uctu";
  74. $body = "<p>Dekujeme Vám za registraci na webu Sa-Host.cz</p>
  75. <p>Pro registraci klikni na tento odkaz: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
  76. <p>S pozdravem tym Sa-Host.cz</p>";
  77.  
  78. $mail = new Mail();
  79. $mail->setFrom(SITEEMAIL);
  80. $mail->addAddress($to);
  81. $mail->subject($subject);
  82. $mail->body($body);
  83. $mail->send();
  84.  
  85. //redirect to index page
  86. header('Location: index.php?action=joined');
  87. exit;
  88.  
  89. //else catch the exception and show the error.
  90. } catch(PDOException $e) {
  91. $error[] = $e->getMessage();
  92. }
  93.  
  94. }
  95.  
  96. }
  97.  
  98. //define page title
  99. $title = 'Demo';
  100.  
  101. //include header template
  102. require('layout/header.php');
  103. ?>
  104.  
  105.  
  106. <div class="container">
  107.  
  108. <div class="row">
  109.  
  110. <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
  111. <form role="form" method="post" action="" autocomplete="off">
  112. <h2>Prosím registrujte se!</h2>
  113. <p>Máš už účet? <a href='login.php'>Přihlášení</a></p>
  114. <hr>
  115.  
  116. <?php
  117. //check for any errors
  118. if(isset($error)){
  119. foreach($error as $error){
  120. echo '<p class="bg-danger">'.$error.'</p>';
  121. }
  122. }
  123.  
  124. //if action is joined show sucess
  125. if(isset($_GET['action']) && $_GET['action'] == 'joined'){
  126. echo "<h2 class='bg-success'>Úspěšně registrován, aktivujte si účet na Emailu.</h2>";
  127. }
  128. ?>
  129.  
  130. <div class="form-group">
  131. <input type="text" name="username" id="username" class="form-control input-lg" placeholder="Uživatelské jméno" value="<?php if(isset($error)){ echo $_POST['username']; } ?>" tabindex="1">
  132. </div>
  133. <div class="form-group">
  134. <input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email " value="<?php if(isset($error)){ echo $_POST['email']; } ?>" tabindex="2">
  135. </div>
  136. <div class="row">
  137. <div class="col-xs-6 col-sm-6 col-md-6">
  138. <div class="form-group">
  139. <input type="password" name="password" id="password" class="form-control input-lg" placeholder="Heslo" tabindex="3">
  140. </div>
  141. </div>
  142. <div class="col-xs-6 col-sm-6 col-md-6">
  143. <div class="form-group">
  144. <input type="password" name="passwordConfirm" id="passwordConfirm" class="form-control input-lg" placeholder="Kontrola hesla" tabindex="4">
  145. </div>
  146. </div>
  147. </div>
  148.  
  149. <div class="row">
  150. <div class="col-xs-6 col-md-6"><input type="submit" name="submit" value="Registrovat" class="btn btn-primary btn-block btn-lg" tabindex="5"></div>
  151. </div>
  152. </form>
  153. </div>
  154. </div>
  155.  
  156. </div>
  157.  
  158. <?php
  159. //include header template
  160. require('layout/footer.php');
  161. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement