0

I am working at a "Change Password" form for my website and I don't know how to encrypt the variable the user enters, "old_pw" to be more specific because later in the code I have to compare it with the password that is in the database. I know that when i create an account, for example, at mysqli_query i can use PASSWORD('$users_password') and it encrypts it for my database. So, i think i need a way to encrypt the old_pw that the user inputs on website to match the password encryption from database. I apologise if my english or if my explination were not good enough.

A picture of my database: Picture of my databse

<?php
    session_start();
    include('config.php');

    $user = $_SESSION['login'];

    if ($user) {

        //user is logged in
        if (isset($_POST['submit'])){

            $old_pw = mysqli_real_escape_string($conn,$_POST['oldpw']);
            $new_pw = mysqli_real_escape_string($conn,$_POST['new_password']);
            $new_pw2 = mysqli_real_escape_string($conn,$_POST['new_password2']);

            $query = mysqli_query($conn, "SELECT password FROM account WHERE login='$user'") or die();
            $row = mysqli_fetch_assoc($query);
            $oldpwdb = $row['password'];

            echo"$old_pw/$oldpwdb";
            //verify if old password from db is the same with old_password that the user has just inputed.

            if ($old_pw == $oldpwdb) {

                // seccond verify -> verifing if the two new passwords are the same.
                if ($new_pw == $new_pw2) {
                    echo"Succes";
                }
                else{
                    die("The two passwords are not the same");
                }
            }
            else{
                echo"old pw from db  doesnt  match old pw from user input";
            }
        }



    }
    else{
        die("Trebuie sa fii logat ca sa schimbi parola.");
    }


?>

<!DOCTYPE html>
<html>

<head lang="ro">
    <!-- adaugare font -->
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Merriweather+Sans&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Caveat&display=swap" rel="stylesheet">  
    <link href="https://fonts.googleapis.com/css?family=Rubik&display=swap" rel="stylesheet">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Galaxy | Schimbare parolă</title>
    <meta name="description" content="Pagina oficiala Metin2Galaxy @2020">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <link rel="icon" href="../images/icon.png">
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</head>

<body>
    <section id="nav-bar">
        <nav class="navbar navbar-expand-lg navbar-light">
            <a class="navbar-brand"><img src="../images/logo.png" alt="logo"></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <i class="fas fa-bars"></i>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav mx-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="welcome.php">Panou utilizator</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="schimbare_parola.php"><span id="active">Schimba parola</span></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="logout.php">Log Out</a>
                    </li>
                </ul>
            </div>
        </nav>
    </section>

<!------------------------------------Text despre----------------------------------->
<section id="banner">
    <div class="container">
        <div class="row">
            <div class="col">
                <h1>Schimbare parola</h1>
                <h2 id="h2_welcome">Completeaza campurile libere pentru a-ti schimba parola.</h2>
                <form  method="post" action="schimbare_parola.php" autocomplete="off">

                    <label for="oldpw"><b>Parola actuala</b></label>
                    <input name="oldpw" maxlength="16" placeholder="Introdu parola actuala a contului" required type="password" autocomplete="new-password" />

                    <label for="new_password"><b>Parola noua</b></label>
                    <input name="new_password" maxlength="16" placeholder="Introdu noua parola a contului" required type="password" autocomplete="new-password" />

                    <label for="new_password2"><b>Repeta parola noua</b></label>
                    <input name="new_password2" maxlength="16" placeholder="Introdu noua parola a contului din nou" required type="password" autocomplete="new-password" />

                    <input type="submit" value="Schimba parola" name="submit" class="registerbtn" />
                    <hr>
                </form>
            </div>
        </div>
    </div>
</section>
nbk
  • 45,398
  • 8
  • 30
  • 47
  • 1
    Side note: you're open to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). You should use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – El_Vanja Mar 29 '20 at 00:14
  • 3
    Use php hashinf functions https://www.php.net/manual/de/ref.password.php and **prepared statements** https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Mar 29 '20 at 00:14
  • @nbk, i can not find the password_hash() parameters to encrypt the way that i need to.. Please can you help me? – Tony Moldovan Mar 29 '20 at 00:23
  • @El_Vanja, thank you for you advice. I will document myself about prepared statements. – Tony Moldovan Mar 29 '20 at 00:26
  • On the php website is example code, and if you have an older version ofg php think about an upgrade. If you have an existing table and you want to use the new password capabilitys , you must more coe e than only chekc if teh oldpasswords is correct and both new passwords are the same, you must enter the new hashed password and also add a marker that the user has switched to the new system. – nbk Mar 29 '20 at 00:37
  • also this explain on site how to hash https://stackoverflow.com/questions/30279321/how-to-use-phps-password-hash-to-hash-and-verify-passwords – nbk Mar 29 '20 at 00:40

0 Answers0