How can users be allowed to set their own passwords in a PHP script?

To allow users to set their own passwords in a PHP script, you can create a form where users can input their desired password and then use PHP to securely hash and store the password in a database. It's important to use a secure hashing algorithm like bcrypt to store passwords safely.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $password = $_POST['password'];
    
    // Hash the password using bcrypt
    $hashed_password = password_hash($password, PASSWORD_BCRYPT);
    
    // Store the hashed password in a database or wherever you need to
    // For example, you can save it to a users table in a database
    // $sql = "INSERT INTO users (username, password) VALUES ('$username', '$hashed_password')";
    
    echo "Password set successfully!";
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="password">Enter your new password:</label><br>
    <input type="password" id="password" name="password"><br><br>
    
    <input type="submit" value="Set Password">
</form>