1
So guys, I want to do the following, I have a site that contains users and such, but when a user wants to change his password I want it to be encrypted to BD, how can I do that? when the user is registered, his password will be encrypted to MYSQL. I will send the registration code:
<?php
session_start();
ob_start();
$btnCadUsuario = filter_input(INPUT_POST, 'btnCadUsuario', FILTER_SANITIZE_STRING);
if($btnCadUsuario){
include_once '../Conexao/conexao.php';
$dados_rc = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$erro = false;
$dados_st = array_map('strip_tags', $dados_rc);
$dados = array_map('trim', $dados_st);
if(in_array('',$dados)){
$erro = true;
$_SESSION['msg'] = "<div class='alert alert-danger'>Necessário preencher todos os campos</div>";
}elseif((strlen($dados['senha'])) < 6){
$erro = true;
$_SESSION['msg'] = "<div class='alert alert-danger'>A senha deve ter no mínimo 6 caracteres</div>";
}elseif(stristr($dados['senha'], "'")) {
$erro = true;
$_SESSION['msg'] = "<div class='alert alert-danger'>Caracter ( ' ) utilizado na senha é inválido</div>";
}else{
$result_usuario = "SELECT idusuario FROM usuarios WHERE usuario='". $dados['usuario'] ."'";
$resultado_usuario = mysqli_query($conn, $result_usuario);
if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
$erro = true;
$_SESSION['msg'] = "<div class='alert alert-danger'>Este usuário já está sendo utilizado</div>";
}
$result_usuario = "SELECT idusuario FROM usuarios WHERE email='". $dados['email'] ."'";
$resultado_usuario = mysqli_query($conn, $result_usuario);
if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
$erro = true;
$_SESSION['msg'] = "<div class='alert alert-danger'>Este e-mail já está sendo utilizado</div>";
}
}
//var_dump($dados);
if(!$erro){
//var_dump($dados);
$dados['senha'] = password_hash($dados['senha'], PASSWORD_DEFAULT);
$result_usuario = "INSERT INTO usuarios (nome, email, usuario, senha) VALUES (
'" .$dados['nome']. "',
'" .$dados['email']. "',
'" .$dados['usuario']. "',
'" .$dados['senha']. "'
)";
$resultado_usario = mysqli_query($conn, $result_usuario);
if(mysqli_insert_id($conn)){
$_SESSION['msgcad'] = "<div class='alert alert-success'>Usuário cadastrado com sucesso!!</div>";
header("Location: LoginPT-BR.php");
}else{
$_SESSION['msg'] = "<div class='alert alert-danger'>Error ao cadastrar usuário!!</div>";
}
}
}
?>
When sending, "UPDATE users SET password = md5('password') Where idusuario = '$id'"; instead of going there, I put this "UPDATE users SET password = password_hash('password') Where idusuario = '$id'"; ?
– Pedro Lukas
password = password_hash('password', PASSWORD_DEFAULT)
– HudsonPH
It would look like this: "UPDATE usuarios SET password = password_hash('password', PASWORD_DEFAULT) Where idusuario = '$id'";, but still will not ; do this error mysqli_error() expects Exactly 1 Parameter, 0 Given in
– Pedro Lukas
"UPDATE usuarios SET password = '" . password_hash('password', PASWORD_DEFAULT). " 'Where
– HudsonPH
just missed to put the ' " .
– HudsonPH
I got a partner, like, he’s encrypting everything right now, but when I try to log in with the changed password it won’t, you know what I’m doing?
– Pedro Lukas
you checked the password in the database, saw how it turned out?
– HudsonPH
I checked yes friend, it was encrypted look only, the password I put was this 9934631254a then in the database was so $2y$10$2s9m6IAg2Mqb1ckZH453.uan2NuFZct4NVpeAHWdOg4lWeL67o5SG that in the case was encrypted but then when I access the account on the website not log in
– Pedro Lukas
but how do you check the login part? vc need to use the same function in password password_hash('password', PASSWORD_DEFAULT)
– HudsonPH
That way if(password_verify($password, $row_user['password'])){
– Pedro Lukas
change to this if your verification function does not convert the password: if(password_verify(password_hash($password, PASWORD_DEFAULT), $row_user['password'])){
– HudsonPH
I just changed, but now when the user creates a password for 1x the same can not enter ;\
– Pedro Lukas
Then I went back to if(password_verify($password, $row_username['password'])){ and I was able to enter, but when I change the password I can’t enter ueheu que brisa mano
– Pedro Lukas