First of all you'll have to excuse my horrible coding skills as I've been studying PHP for ~1 week now. Following the advice of people who know better than me, I ask for review so that I don't "learn to write bad code from the beginning". Long story short, here's the code. Any advice is welcomed. What can be improved and so on.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register</title>
</head>
<body>
<form name="input" action="" method="POST" >
Username: <br> <input type="text" name="user"><br>
Password:
<br> <input type="text" name="pass"><br>
Retype password:
<br> <input type="text" name="rpass"><br>
Email: <br> <input type="text" name="email"><br>
<input type="submit" name="submit" value="Register">
</form>
</body>
<?php
if(isset($_POST['submit'])){
//get values from FORM
$user = $_POST['user'];
$pass = $_POST['pass'];
$rpass = $_POST['rpass'];
$email = $_POST['email'];
$email_regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
//check if either field is empty
if (empty($user) || empty($pass) || empty($rpass) || empty($email)){
echo '<font size="3" color="red">You must fill in all fields.</font><br>';
die;
}
//check if files exist
if(!require 'functions.php'){
die('File not found!');
}
$_regError = new registerErrors;
//Assign checks to credentials
$credentials = array(
'username_valid' => preg_match("/^[a-z0-9._-]*$/i", ($user)),
'pass_match' => ($pass === $rpass),
'pass_len' => ($pass < 6),
'email_valid' => preg_match($email_regex, $email)
);
$errors = array();
foreach($credentials as $key=>$isvalid){
if(!$isvalid){
switch($key){
case 'username_valid':
$errors [] = $_regError -> userInvalid();
break;
case 'pass_match':
$errors [] = $_regError->passwordNoMatch();
break;
case 'pass_len':
$errors [] = $_regError -> passwordNoMatch();
break;
case 'email_valid':
$errors [] = $_regError -> emailInvalid();
break;
}
}
//foreach ends here
}
if(count($errors) == 0){
$db = new PDO('mysql:host=localhost;dbname=xxxxt', 'xxx', 'xxxx');
if(!$db){
echo 'Unable to connect to database.';
die;
}
$pass_hash = new hashPassword();
$hash = $pass_hash->hashPass($pass);
$query_user = $db->prepare("SELECT * FROM users WHERE `user` =?");
$query_email = $db->prepare("SELECT * FROM users WHERE `email` =?");
$query_user->execute(array($user));
$query_email->execute(array($email));
if(($query_user->rowCount() > 0) || ($query_email->rowCount() > 0)){
$_regError->userExists();
}else{
$query = $db->prepare("INSERT INTO users (user, pass, email) VALUES ('$user', '$hash', '$email')");
$query->execute();
echo '<font size="3" color="red">Registration succesful!<br>';
}
//count errors ends here
}
//check (!submit) ends here
}
?>
<?php
class registerErrors{
public function passwordNoMatch(){
echo '<font size="3" color="red">Passwords must match and be at least 6 characters in lenght.</font><br>';
die();
}
public function emailInvalid(){
echo '<font size="3" color="red">The Email is invalid.<br>';
die();
}
public function userInvalid(){
echo '<font size="3" color="red">The Username is invalid.<br>';
die();
}
public function userExists(){
echo '<font size="3" color="red">The Username or email already exists.<br>';
}
//registerErrors ends here
}
class hashPassword{
public function hashPass($pass){
//crypt password ! CHANGE THIS TO BCRYPT WHEN YOU GET THE TIME !
$salt = substr($pass, (strlen($pass) / 2));
$hash = MD5($pass, $salt );
return $hash;
}
}
?>