I created code for my registration form on my social network. I want to know if it is secured enough to release on the web as a public social network. Also, all of the code works. I just want to make sure it's all secure.
registration.php:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//check if form is submitted
if ( $_SERVER['REQUEST_METHOD'] != 'POST' || ! isset($_POST['Register']))
{
// looks like a hack, send to index.php
header('Location: index.php');
die();
}
include("connect.php");
if(isset($_POST['Register'])) {
if (empty($_POST["username"])) {
echo"Fill in username to sign up";
} else {
if (empty($_POST["pw"])) {
echo"Fill in password to sign up";
} else {
if (empty($_POST["pw2"])) {
echo"Confirm password to sign up";
} else {
if (empty($_POST["email"])) {
echo"Fill in email to sign up";
} else {
if ($_POST['pw'] == $_POST['pw2']) {
$username = mysqli_real_escape_string($conn, $_POST["username"]);
$pw= mysqli_real_escape_string($conn, $_POST["pw"]);
$pw2= mysqli_real_escape_string($conn, $_POST["pw2"]);
$email = mysqli_real_escape_string($conn, $_POST["email"]);
$result = mysqli_query($conn ,"SELECT * FROM users WHERE username='" . $username . "'");
if(mysqli_num_rows($result) > 0)
{
echo "Username exists . <a href= index.php>Try again</a><br /> ";
} else {
$result2 = mysqli_query($conn ,"SELECT * FROM users WHERE email='" . $email. "'");
if(mysqli_num_rows($result2) > 0)
{
echo "Email exist. <a href= index.php>Try again</a><br /> ";
} else {
$pw = password_hash($pw, PASSWORD_BCRYPT, array('cost' => 14));
$stmt = $conn->prepare("INSERT INTO users (username, pw, email) VALUES(?, ?, ?)");
$stmt->bind_param("sss", $username, $pw, $email);
$username = $username;
$pw = $pw;
$email = $email;
$stmt->execute();
echo"Registration successful <a href= index.php>Login here</a><br />";
} } } else{
echo "The passwords do not match."; // and send them back to registration page
}
}
}}}}
login.php:
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("connect.php");
$_SESSION['username'] = $_POST['username'];
//check if form is submitted
if ( $_SERVER['REQUEST_METHOD'] != 'POST' || ! isset($_POST['signin']))
{
// looks like a hack, send to index.php
header('Location: index.php');
die();
}
if (empty($_POST["username"])) {
echo 'Fill in username to sign in. <a href= index.php>Try again</a><br />';
die();
}
if (empty($_POST["pw"])) {
echo 'Fill in password to sign in. <a href= index.php>Try again</a><br />';
die();
}
$sql = "SELECT pw FROM users WHERE username = ?";
$stmt = mysqli_prepare($conn, $sql);
if ( !$stmt ) {
echo mysqli_error($conn);
die();
}
$stmt->bind_param('s', $_POST['username']);
if ( !$stmt->execute() ) {
echo mysqli_error($conn);
die();
}
// we found a row with that username,
// now we need to check the password is correct
// get the password from the row
$stmt->bind_result($hashed_pwd);
$stmt->fetch();
if ( password_verify($_POST['pw'], $hashed_pwd) ) {
// password verified
$_SESSION["username"] = $_POST['username'];
header('Location: profile.php');
} else {
echo 'Incorrect username or Password. <a href= index.php>Try again</a><br />';
}