简体   繁体   中英

How to hash password register?

I have form for register and password column named password (in index.php), but the php said no variable called "password"

Notice: Undefined variable: password in D:\\laragon\\www\\mbl2\\web_mbl1\\register.php on line 20

<div class="modal" id="mymodal2" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Register</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <form action="register.php" method="POST">
                    <div class="form-group">
                        <input type="text" name="username" placeholder="Username" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <input type="email" name="email" placeholder="Email" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <input type="phone" name="nohp" placeholder="Phone Number" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <input type="password" name="password" placeholder="Password" class="form-control" required>
                        <button type="submit" name="submitregister" value="submit">submit</button>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<?php
session_start(); // Starting Session

if (isset($_SESSION['login_id'])) {
    if (isset($_SESSION['pageStore'])) {
        $pageStore = $_SESSION['pageStore'];
        header("location: $pageStore"); // Redirecting To Profile Page
    }
}

if (isset($_POST['submitregister'])) {
    if (empty($_POST['username']) || empty($_POST['email']) || empty($_POST['nohp'])) {
        echo "Please fill up all the required field.";
    } else {
        $username = $_POST['username'];
        $email    = $_POST['email'];
        $nohp     = $_POST['nohp'];
        $hash     = password_hash($password, PASSWORD_DEFAULT);


        include('connection.php');
        $sQuery = "SELECT id from register where email=? LIMIT 1";
        $iQuery = "INSERT Into register (username, email,nohp, password) values(?,?, ?, ?)";

        $stmt = $conn->prepare($sQuery);
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->bind_result($id);
        $stmt->store_result();
        $rnum = $stmt->num_rows;
        if ($rnum == 0) { //if true, insert new data
            $stmt->close();

            $stmt = $conn->prepare($iQuery);
            $stmt->bind_param("ssis", $fullName, $email, $nohp, $hash);
            if ($stmt->execute()) {
                echo 'Register successfully, Please login with your login details';
            }
        } else {
            echo 'Someone already register with this email address.';
        }
        $stmt->close();
        $conn->close(); // Closing database Connection
    }
}
?>

index.php has register form that connected with register.php , but the register.php couldn't find the variable

The code fails because the $password is undefined. This can be fixed by placing the following snippet:

$password = $_POST['password'];

above:

 $hash     = password_hash($password, PASSWORD_DEFAULT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM