Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Q&A

How To Verify Old Password in PHP? [closed]

+0
−1

Closed as unclear by Alexei‭ on Mar 29, 2022 at 08:40

This question cannot be answered in its current form, because critical information is missing.

This question was closed; new answers can no longer be added. Users with the reopen privilege may vote to reopen this question if it has been improved or closed incorrectly.

  • I have a page to change the user password in my website, this page can only be used if the user have a logged in!

  • The change password page have 3 inputs: Old Password, New Password and Confirm Password each one will verify if it is empty.

  • I tried using the password_verify method but it didn't worked

I wanted to be able to check if the user old password is valid

Here is some relevant code:

<?php
// Inicializar a sessão
session_start();

// Verificar se o utilizador está ligado, se não estiver, redireccioná-lo para a página de início de sessão
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
  header("location: login.php");
  exit;
}

// Incluir ficheiro de configuração
require_once "config.php";

// Definir variáveis e inicializar com valores vazios
$old_password = $old_password_err = "";
$new_password = $confirm_password = "";
$new_password_err = $confirm_password_err = "";

// Processamento de dados do formulário quando o formulário é submetido
if ($_SERVER["REQUEST_METHOD"] == "POST") {

  // Validar Palavra-Passe atual
  if (empty(trim($_POST["old_password"]))) {
    $old_password_err = "Este campo é obrigatório!";
  } else {
    $old_password = trim($_POST["old_password"]);
  }

  //Validar se a Palavra-Passe está correta!
  //code

  // Validar Palavra-Passe
  if (empty(trim($_POST["new_password"]))) {
    $new_password_err = "Este campo é obrigatório!";
  } elseif (strlen(trim($_POST["new_password"])) < 6) {
    $new_password_err = "A Palavra-Passe deve ter pelo menos 6 caracteres.";
  } else {
    $new_password = trim($_POST["new_password"]);
  }

  // Validar confirmação de Palavra-Passe
  if (empty(trim($_POST["confirm_password"]))) {
    $confirm_password_err = "Este campo é obrigatório!";
  } else {
    $confirm_password = trim($_POST["confirm_password"]);
    if (empty($new_password_err) && ($new_password != $confirm_password)) {
      $confirm_password_err = "A Palavra-Passe não correspondeu.";
    }
  }

  // Verificar erros antes de atualizar a base de dados
  if (empty($old_password_err) && empty($new_password_err) && empty($confirm_password_err)) {
    // Preparar uma declaração de inserção
    $sql = "UPDATE users SET password = ? WHERE id = ?";

    if ($stmt = mysqli_prepare($link, $sql)) {
      // Ligar variáveis à declaração preparada como parâmetros
      mysqli_stmt_bind_param($stmt, "si", $param_password, $param_id);

      // Definir parâmetros
      $param_password = password_hash($new_password, PASSWORD_DEFAULT);
      $param_id = $_SESSION["id"];

      // Tentatar executar a declaração preparada
      if (mysqli_stmt_execute($stmt)) {
        // Palavra-Passe atualizada, destruir sessão e redirecionar para página de login
        session_destroy();
        header("location: login.php");
        exit();
      } else {
        echo "Algo de errado não está certo!";
      }

      // Declaração de encerramento
      mysqli_stmt_close($stmt);
    }
  }

  // Ligação fechada
  mysqli_close($link);
}
?>
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General feedback (1 comment)

0 answers

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate