checking password confirmation in php

207 views Asked by At
<?php
include("../../config/database_connection.php");

if( isset($_POST) )
{
    $user_name   =$_POST['user_name'];
    $email   = $_POST['email'];
    $user_pass_init    = $_POST['password'];
    $user_pass_conf = $_POST['passconfirm'];
    $full_name = $_POST['full_name'];
    $gender = $_POST['gender'];

    if ($user_name!="" && $email!="" && $full_name!="" && $gender!="") {
        if ($_POST['password']!= $_POST['passconfirm']) {
            header("location:../index.php?err= password do not match");
        }else{
            $query   = "INSERT into admin_users (user_name,email,user_pass,full_name,gender) VALUES('" . $user_name . "','" . $email . "','" . md5($user_pass_init) . "','" . $full_name . "','" . $gender . "')";
            $success = $conn->query($query);
        }

        if (!$success) {
            die("Couldn't enter data: ".$conn->error);
        } else{
            header("location:../index.php");
        }
    } else{
        header("location:../index.php?err= Enter all the fields");
    }
} else{
    header("location:../index.php?err= couldnot enter data");
}
?>

this is the code I am trying to execute to check whether two passwords are matching or not... but while entering same passwords I get value of password do not match Whats the issue? Help me solve it

1

There are 1 answers

1
Solomon Antoine On BEST ANSWER

Here is my recommendation, this may not completely solve your issue but it will make your code a bit more secure since your code is susceptible to SQL injection:

<?php
include("../../config/database_connection.php");
if( isset($_POST) )
{
    $user_name  = $conn->real_escape_string($_POST['user_name']);
    $email   = $conn->real_escape_string($_POST['email']);
    $user_pass_init    = $conn->real_escape_string($_POST['password']);
    $user_pass_conf = $conn->real_escape_string($_POST['passconfirm']);
    $full_name = $conn->real_escape_string($_POST['full_name']);
    $gender = $conn->real_escape_string($_POST['gender']);

    if (!empty($user_name) && !empty($email) && !empty($full_name) && !empty($gender)) {
        if ($user_pass_init != $user_pass_conf) {
            header("location:../index.php?err= password do not match");
        }else{
            $user_pass = md5($user_pass_init);
            $query   = "INSERT into admin_users (user_name,email,user_pass,full_name,gender) VALUES('$user_name', '$email', '$user_pass', '$full_name', '$gender')";
            $success = $conn->query($query);
            if (!$success) {
                die("Couldn't enter data: ".$conn->error);
            } else{
                header("location:../index.php");
            }
        }
    } else{
        header("location:../index.php?err= Enter all the fields");
    }
} else{
    header("location:../index.php?err= couldnot enter data");
}
?> 

I also would recommend using password_hash() instead of md5()