I was following a tutorial for a social network. I want to know how secure this code is because I plan on launching it to the public web.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// declaring vars to prevent errors
$fname = ""; //First name
$lname = ""; //Last name
$em = ""; //Email
$em2 = ""; // Email 2
$password = ""; //password
$password2 = ""; //password 2
$date = ""; // sign up date
$error_array = array(); //holds error messages
if(isset($_POST['register_button'])) {
// registration form values
// first name
$uname = strip_tags($_POST['reg_uname']); // remove html tags
$uname = str_replace(' ', '', $uname); //remove spaces
$_SESSION['reg_uname'] = $uname; // stores first name in session variable
// first name
$fname = strip_tags($_POST['reg_fname']); // remove html tags
$fname = str_replace(' ', '', $fname); //remove spaces
$fname = ucfirst(strtolower($fname)); //uppercase first letter
$_SESSION['reg_fname'] = $fname; // stores first name in session variable
// Last name
$lname = strip_tags($_POST['reg_lname']); // remove html tags
$lname = str_replace(' ', '', $lname); //remove spaces
$lname = ucfirst(strtolower($lname)); //uppercase first letter
$_SESSION['reg_lname'] = $lname; // stores last name in session variable
// E-mail
$em = strip_tags($_POST['reg_email']); // remove html tags
$em = str_replace(' ', '', $em); //remove spaces
$em = ucfirst(strtolower($em)); //uppercase first letter
$_SESSION['reg_email'] = $em; // stores email in session variable
// E-mail 2
$em2 = strip_tags($_POST['reg_email2']); // remove html tags
$em2 = str_replace(' ', '', $em2); //remove spaces
$em2 = ucfirst(strtolower($em2)); //uppercase first letter
$_SESSION['reg_email2'] = $em2; // stores email2 in session variable
// Password
$password = strip_tags($_POST['reg_password']); // remove html tags
$password2 = strip_tags($_POST['reg_password2']); // remove html tags
$date = date("Y-m-d"); //current date
if($em == $em2) {
//check if email is in valid format
if(filter_var($em, FILTER_VALIDATE_EMAIL)){
$em = filter_var($em, FILTER_VALIDATE_EMAIL);
// check if email already exists
$e_check = mysqli_query($con, "SELECT email FROM users WHERE email='$em'");
// Count the number of rows returned
$num_rows = mysqli_num_rows($e_check);
if($num_rows > 0){
array_push($error_array, "E-mail already in use<br>");
}
} else{
array_push($error_array, "Invalid email format<br>");
}
} else {
array_push($error_array, "E-mails don't match<br>");
}
if(strlen($uname) > 25 || strlen($uname) < 1) {
array_push($error_array, "Your username must be between 2 and 25 characters<br>");
}
if(strlen($fname) > 25 || strlen($fname) < 1) {
array_push($error_array, "Your first name must be between 2 and 25 characters<br>");
}
if(strlen($lname) > 25 || strlen($lname) < 1) {
array_push($error_array, "Your last name must be between 2 and 25 characters<br>");
}
if($password != $password2){
array_push($error_array, "Your passwords do not match<br>");
} else {
if(preg_match('/[^A-Za-z0-9]/', $password)) {
array_push($error_array, "Your password can only contain english characters or numbers.<br>");
}
}
if(strlen($password > 30 || strlen($password) < 5)){
array_push($error_array, "Your password must be between 5 and 30 characters<br>");
}
if(empty($error_array)) {
// $password = password_hash($password); // encrypt password
$password = password_hash($_POST['reg_password'], PASSWORD_BCRYPT, array('cost' => 14));
// profile picture assignment
$profile_pic = "assets/images/profile_pics/defaults/default.jpg";
$query = mysqli_query($con, "INSERT INTO users VALUES(NULL, '$fname', '$lname', '$uname', '$em',
'$password', '$date', '$profile_pic', '0', '0', 'no', ',')");
array_push($error_array, "<span style='color: #669999' >You can now login</span><br>");
//clear session variables
$_SESSION['reg_fname'] = "";
$_SESSION['reg_uname'] = "";
$_SESSION['reg_lname'] = "";
$_SESSION['reg_email'] = "";
$_SESSION['reg_email2'] = "";
}
}
?>