Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php require('includes/config.php');
- //if logged in redirect to members page
- if( $user->is_logged_in() ){ header('Location: memberpage.php'); }
- //if form has been submitted process it
- if(isset($_POST['submit'])){
- //very basic validation
- if(strlen($_POST['username']) < 3){
- $error[] = 'Username is too short.';
- } else {
- $stmt = $db->prepare('SELECT username FROM members WHERE username = :username');
- $stmt->execute(array(':username' => $_POST['username']));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if(!empty($row['username'])){
- $error[] = 'Username provided is already in use.';
- }
- }
- if(strlen($_POST['password']) < 3){
- $error[] = 'Heslo musí obsahovat 3+ znaků!';
- }
- if(strlen($_POST['passwordConfirm']) < 3){
- $error[] = 'Heslo musí obsahovat 3+ znaků!';
- }
- if($_POST['password'] != $_POST['passwordConfirm']){
- $error[] = 'Hesla se neschodují!';
- }
- //email validation
- if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
- $error[] = 'Prosím zadej platný email.';
- } else {
- $stmt = $db->prepare('SELECT email FROM members WHERE email = :email');
- $stmt->execute(array(':email' => $_POST['email']));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if(!empty($row['email'])){
- $error[] = 'Tento email již byl použit!';
- }
- }
- //if no errors have been created carry on
- if(!isset($error)){
- //hash the password
- $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);
- //create the activasion code
- $activasion = md5(uniqid(rand(),true));
- try {
- //insert into database with a prepared statement
- $stmt = $db->prepare('INSERT INTO members (username,password,email,active) VALUES (:username, :password, :email, :active)');
- $stmt->execute(array(
- ':username' => $_POST['username'],
- ':password' => $hashedpassword,
- ':email' => $_POST['email'],
- ':active' => $activasion
- ));
- $id = $db->lastInsertId('memberID');
- //send email
- $to = $_POST['email'];
- $subject = "Registrace uctu";
- $body = "<p>Dekujeme Vám za registraci na webu Sa-Host.cz</p>
- <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>
- <p>S pozdravem tym Sa-Host.cz</p>";
- $mail = new Mail();
- $mail->setFrom(SITEEMAIL);
- $mail->addAddress($to);
- $mail->subject($subject);
- $mail->body($body);
- $mail->send();
- //redirect to index page
- header('Location: index.php?action=joined');
- exit;
- //else catch the exception and show the error.
- } catch(PDOException $e) {
- $error[] = $e->getMessage();
- }
- }
- }
- //define page title
- $title = 'Demo';
- //include header template
- require('layout/header.php');
- ?>
- <div class="container">
- <div class="row">
- <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
- <form role="form" method="post" action="" autocomplete="off">
- <h2>Prosím registrujte se!</h2>
- <p>Máš už účet? <a href='login.php'>Přihlášení</a></p>
- <hr>
- <?php
- //check for any errors
- if(isset($error)){
- foreach($error as $error){
- echo '<p class="bg-danger">'.$error.'</p>';
- }
- }
- //if action is joined show sucess
- if(isset($_GET['action']) && $_GET['action'] == 'joined'){
- echo "<h2 class='bg-success'>Úspěšně registrován, aktivujte si účet na Emailu.</h2>";
- }
- ?>
- <div class="form-group">
- <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">
- </div>
- <div class="form-group">
- <input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email " value="<?php if(isset($error)){ echo $_POST['email']; } ?>" tabindex="2">
- </div>
- <div class="row">
- <div class="col-xs-6 col-sm-6 col-md-6">
- <div class="form-group">
- <input type="password" name="password" id="password" class="form-control input-lg" placeholder="Heslo" tabindex="3">
- </div>
- </div>
- <div class="col-xs-6 col-sm-6 col-md-6">
- <div class="form-group">
- <input type="password" name="passwordConfirm" id="passwordConfirm" class="form-control input-lg" placeholder="Kontrola hesla" tabindex="4">
- </div>
- </div>
- </div>
- <div class="row">
- <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>
- </div>
- </form>
- </div>
- </div>
- </div>
- <?php
- //include header template
- require('layout/footer.php');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement