-1
Good morning..
I tried to hash using php password_hash and did not succeed, it even generates the hash, but the problem is time to check the hash. Can someone help me?
Login and Create function
public function login($email,$senha){
$query = $this->pdo->prepare("SELECT * FROM usuarios WHERE email= :email AND senha= :senha");
$query->bindValue(':email', $email);
$query->bindValue(':senha', $senha);
$execute = $query->execute();
if($execute){
$fetch = $query->fetchAll(PDO::FETCH_OBJ);
if(count($fetch)>0){
$q = $this->pdo->prepare("SELECT * FROM usuarios WHERE email= :email AND senha= :senha");
$q->bindValue(':email', $email);
$q->bindValue(':senha', $senha);
$q->execute();
$fetch = $q->fetch(PDO::FETCH_OBJ);
$_SESSION['USER'] = (array)$fetch;
return true;
}else{
return false;
}
}else{
return false;
}
}
public Function create($email,$password,$Whatsapp,$name){
$query = $this->pdo->prepare("INSERT INTO usuarios (nome,email,whatsapp,senha) VALUES (:nome,:email,:whatsapp,:senha) ");
$query->bindValue(':nome', $nome);
$query->bindValue(':email', $email);
$query->bindValue(':whatsapp', $whatsapp);
$query->bindValue(':senha', $senha);
if($query->execute()){
return true;
}else{
return false;
}
}
Here the part of creating user with hash
if(isset($_POST['email']) && isset($_POST['whatsapp']) && isset($_POST['senha']) && isset($_POST['nome'])){
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$senha = password_hash($_POST['senha'], PASSWORD_DEFAULT);
$whatsapp = $_POST['whatsapp'];
$nome = $_POST['nome'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo '{"erro":true,"create":0,"login":0,"msg":"Email inválido, verifique-o."}';
exit;
}
if(!is_numeric($whatsapp)){
echo '{"erro":true,"create":0,"login":0,"msg":"Tefone inválido, verifique o número."}';
exit;
}
if ($nome === '') {
echo '{"erro":true,"create":0,"login":0,"msg":"Por favor, Preencha o nome."}';
exit;
}
if ($whatsapp === '') {
echo '{"erro":true,"create":0,"login":0,"msg":"Por favor, Preencha o telefone."}';
exit;
}
if ($email === '') {
echo '{"erro":true,"create":0,"login":0,"msg":"Por favor, Preencha o email."}';
exit;
}
if ($senha === '') {
echo '{"erro":true,"create":0,"login":0,"msg":"Por favor, Preencha a senha."}';
exit;
}
$verify = $clientes->verify_email($email);
if ($verify > 0) {
echo '{"erro":true,"create":0,"login":0,"msg":"Este endereço de e-mail já está sendo usado por outro usuário."}';
exit;
}
$create = $clientes->create($email,$senha,$whatsapp,$nome);
if($create){
$login = $clientes->login($email,$senha);
if($login){
echo '{"erro":false,"create":1,"login":1,"msg":"logado"}';
}else{
echo '{"erro":true,"create":1,"login":0,"msg":"Sua conta foi criada, faça login"}';
}
}else{
echo '{"erro":true,"create":0,"login":0,"msg":"Erro temporario, tente mais tarde, ou entre em contato com o suporte"}';
}
}
Here the login part
if(isset($_POST['email'])){
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$senha = $_POST['senha'];
$login = $clientes->login($email,$senha);
if($login){
if(isset($_SESSION['checkout'])){
echo '3';
}else{
echo '1';
}
}else{
echo '0';
}
}
NOTE: The checking part didn’t put pq there every way I tried the login error.
PS: The code is not tested and may contain some error. I have no way to test, since I no longer use PHP, and I do not have it installed.
– Inkeliz
Thank you, I tested the code, there was error in rowCount. I removed and authenticated, there is some problem to remove Count?
– Greg Cruzeiro
Seeing the documentation, the
rowCount
PDO is not equal tonumRows
Mysqli, there are no guarantees that always works on SELECT in all databases. There is no problem in removing it, however, it is important to know if there is any record or not. I changed the code toif(!is_array($result)){ return false; }
, hoping to work, but it’s a kick.– Inkeliz
perfect. Thank you very much
– Greg Cruzeiro
Looks like they closed the question, I can’t get the best answer
– Greg Cruzeiro