Dtabase.php
<?php
class Database
{
private $hostdb = "localhost";
private $userdb = "root";
private $passworddb = "";
private $namedb = "color-school";
public $pdo;
public function __construct()
{
if (!isset($this->pdo)) {
try {
$link = new PDO("mysql:host=" . $this->hostdb . ";dbname=" . $this->namedb, $this->userdb, $this->passworddb);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$link->exec("SET CHARACTER SET utf8");
$this->pdo = $link;
} catch (PDOException $e) {
die("Fail to connect with Database " . $e->getMessage());
}
}
}
}
register.php
<?php
include 'includes/header.php';
include 'library/User.php';
?>
<?php
$user = new User();
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['register'])){
$userRegistration = $user->userRegistration($_POST);
}
?>
<div class="panel panel-default">
<div class="panel-heading">
<h2>User Registration</h2>
</div>
<div class="panel-body">
<div style="max-width: 600px; margin: 0 auto">
<?php
if(isset($userRegistration)){
echo $userRegistration;
}
?>
<form action="" method="post">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" class="form-control">
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" class="form-control">
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="text" id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" class="form-control">
</div>
<button type="submit" name="register" class="btn btn-success">Submit</button>
</form>
</div>
</div>
</div>
<?php include 'includes/footer.php' ?>
User.php
<?php
include_once 'Session.php';
include 'Database.php';
class User
{
private $db;
public function __construct()
{
$this->db = new Database();
}
public function userRegistration($data)
{
$name = $data['name'];
$username = $data['username'];
$email = $data['email'];
$password = md5($data['password']);
$chk_email = $this->emailCheck($email);
if ($name == "" || $username == "" || $email = "" || $password = "") {
$message = "<div class='alert alert-danger'><strong>Error! </strong>Field must not be empty</div>";
return $message;
}
if (strlen($username) < 3) {
$message = "<div class='alert alert-danger'><strong>Error! </strong>User name is to short!</div>";
return $message;
} elseif (preg_match('/[^a-z0-9_-]+/i', $username)) {
$message = "<div class='alert alert-danger'><strong>Error! </strong>Username must only contain alphanumerical, dashes and underscores!</div>";
return $message;
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$message = "<div class='alert alert-danger'><strong>Error! </strong>The email is not valid!</div>";
return $message;
}
if ($chk_email == true) {
$message = "<div class='alert alert-danger'><strong>Error! </strong>The email address already exist!</div>";
return $message;
}
$sql = "INSERT INTO users(name, username, email, password) VALUES(:name, :username, :email, :password)";
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':name', $name);
$query->bindValue(':username', $username);
$query->bindValue(':email', $email);
$query->bindValue(':password', $password);
$result = $query->execute();
if ($result) {
$message = "<div class='alert alert-success'><strong>Success! </strong>Thank you, you have been registered.</div>";
return $message;
} else {
$message = "<div class='alert alert-danger'><strong>Error! </strong>Sorry, there has been problem inserting your details.</div>";
return $message;
}
}
public function emailCheck($email)
{
$sql = "SELECT email FROM users WHERE email=:email";
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':email', $email);
$query->execute();
if ($query->rowCount() > 0) {
return true;
} else {
return false;
}
}
}