Advertisement
Guest User

Untitled

a guest
Oct 9th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.01 KB | None | 0 0
  1. <?php require('includes/global.php');
  2. include ('includes/header.php');
  3.  
  4. //if logged in redirect to members page
  5. if( $user->is_logged_in() ){ header('Location: memberpage.php'); }
  6.  
  7. //if form has been submitted process it
  8. if(isset($_POST['submit'])){
  9.    
  10.     //very basic validation
  11.     if(strlen($_POST['username']) < 3){
  12.         $error[] = 'Username is too short.';
  13.     } else {
  14.         $stmt = $db->prepare('SELECT username FROM members WHERE username = :username');
  15.         $stmt->execute(array(':username' => $_POST['username']));
  16.         $row = $stmt->fetch(PDO::FETCH_ASSOC);
  17.         if(!empty($row['username'])){
  18.             $error[] = 'Username provided is already in use.';
  19.         }
  20.     }
  21.     if(strlen($_POST['password']) < 1){
  22.         $error[] = 'Password is too short.';
  23.     }
  24.     if(strlen($_POST['passwordConfirm']) < 1){
  25.         $error[] = 'Confirm password is too short.';
  26.     }
  27.     if($_POST['password'] !== $_POST['passwordConfirm']){
  28.         $error[] = 'Passwords do not match.';
  29.     }
  30.    
  31.     //email validation
  32.     if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
  33.         $error[] = 'Please enter a valid email address';
  34.     } else {
  35.         $stmt = $db->prepare('SELECT email FROM members WHERE email = :email');
  36.         $stmt->execute(array(':email' => $_POST['email']));
  37.         $row = $stmt->fetch(PDO::FETCH_ASSOC);
  38.         if(!empty($row['email'])){
  39.             $error[] = 'Email provided is already in use.';
  40.         }
  41.     }
  42.    
  43.     //if no errors have been created carry on
  44.     if(!isset($error)){
  45.        
  46.         //hash the password
  47.         $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);
  48.        
  49.         //create the activasion code
  50.         $activasion = md5(uniqid(rand(),true));
  51.         try {
  52.            
  53.             //insert into database with a prepared statement
  54.             $stmt = $db->prepare('INSERT INTO members (username,password,email,active) VALUES (:username, :password, :email, :active)');
  55.             $stmt->execute(array(
  56.                 ':username' => $_POST['username'],
  57.                 ':password' => $hashedpassword,
  58.                 ':email' => $_POST['email'],
  59.                 ':active' => $activasion
  60.             ));
  61.             $id = $db->lastInsertId('memberID');
  62.            
  63.             //send email
  64.             $to = $_POST['email'];
  65.             $subject = "Registration Confirmation";
  66.             $body = "<p>Thank you for registering at Elcro Development.</p>
  67.             <p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
  68.             <p>Elcro</p>";
  69.             $mail = new Mail();
  70.             $mail->setFrom(SITEEMAIL);
  71.             $mail->addAddress($to);
  72.             $mail->subject($subject);
  73.             $mail->body($body);
  74.             $mail->send();
  75.            
  76.             //redirect to index page
  77.             header('Location: index.php?action=joined');
  78.             exit;
  79.            
  80.         //else catch the exception and show the error.
  81.         } catch(PDOException $e) {
  82.             $error[] = $e->getMessage();
  83.         }
  84.     }
  85. }
  86.  
  87. ?>
  88.  
  89. <div id="bodyWrapper">
  90.     <div class="global-form" id="registerform">
  91.         <div class="container">
  92.             <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
  93.                 <form action="register.php" method="POST" role="form" autocomplete="off">
  94.                     <?php
  95.                     //check for any errors
  96.                     if(isset($error)){
  97.                         foreach($error as $error){
  98.                             echo '<p class="alert alert-danger">'.$error.'</p>';
  99.                         }
  100.                     }
  101.                     //if action is joined show sucess
  102.                     if(isset($_GET['action']) && $_GET['action'] == 'joined'){
  103.                         echo "<h2 class='alert alert-success'>Registration successful, please check your email to activate your account.</h2>";
  104.                     }
  105.                 ?>
  106.                     <img src="https://elcrodevelopment.com/checkout/assets/imgs/logo.png" alt="Logo" width="250" style="padding-bottom: 10px;text-align:center;">
  107.                     <p class="form-intro" style="font-size:22px;color:#000;">Register on Elcro Development</p>
  108.                     <p style="color:#000;"><?= $message ?></p>
  109.                     <fieldset class="form-group">
  110.                         <input type="text" class="form-control" name="username" placeholder="Username" value="<?php if(isset($error)){ echo $_POST['username']; } ?>" required>
  111.                     </fieldset>
  112.                     <fieldset class="form-group">
  113.                         <input type="text" class="form-control" name="email" placeholder="Email" value="<?php if(isset($error)){ echo $_POST['email']; } ?>" required>
  114.                     </fieldset>
  115.                     <fieldset class="form-group">
  116.                         <div class="col-xs-6 col-sm-6 col-md-6">
  117.                             <input type="password" class="form-control" name="password" placeholder="Password" required>
  118.                         </div>
  119.                         <div class="col-xs-6 col-sm-6 col-md-6">
  120.                             <input type="password" class="form-control" name="confirmpass" placeholder="Confirm Password" required>
  121.                         </div>
  122.                     </fieldset>
  123.                     <fieldset class="form-group">
  124.                         <div class="checkbox">
  125.                             <label><input type="checkbox" name="terms" value="accept">I accept the <a href="https://elcrodevelopment.com/terms.php" target="_blank">Terms & Conditions</a>.</label>
  126.                         </div>
  127.                         <p style="text-align: center;">Already have an account? <a href="login.php">Go to login</a></p>
  128.                     </fieldset>
  129.                     <button type="submit" name="submit" id="clientbutton" value="Register" class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3 btn btn-primary">Register</button>
  130.                 </form>
  131.             </div>
  132.         </div>
  133.     </div>
  134. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement