Reputation: 363
I have this html form:
<form class="signup-form" action="action/register.php" method="post">
<input type="text" class="input" name="user" id="user_name" autocomplete="off" placeholder="Username" required="true">
<input type="text" class="input" name="nome" id="name" autocomplete="off" placeholder="Nome" required="true">
<input type="password" class="input" name="pass" id="user_pass" autocomplete="off" placeholder="Password" required="true">
<input type="password" class="input" name="cpass" id="user_cpass" autocomplete="off" placeholder="Confirme a Password" required="true">
<input type="submit" class="button" name="submit" value="Sign Up">
</form
and the action/register.php is like this:
global $db;
$username=trim($_POST['user']);
$nome=trim($_POST['nome']);
$password=trim($_POST['pass']);
$cpassword=trim($_POST['cpass']);
if(strcmp($password,$cpassword)!==0) {
echo "<script> window.location.assign('../errors/password_error.php'); </script>";
//header('Location: ../errors/password_error.php');
}
$stmt = $db->prepare('SELECT username FROM utilizador');
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
if($row['username'] === $username){
header('Location: ../errors/username_error.php');
}
}
when I insert a username that already exists, it redirects me successfully to errors/username_error.php, but when I insert a cpass different from pass, it accepts it, when it should not (I've tried both with the echo and with the header).
Any suggestions?
PS: I've already tried if($password !== $cpassword)
PPS: I think I solved it, but I don't understand why, do you? this is the whole register.php that I had:
<?php
include_once('../database/connection.php'); // connects to the database
session_start(); // starts the session
function NewUser()
{
global $db;
$username=trim($_POST['user']);
$nome=trim($_POST['nome']);
$password=trim($_POST['pass']);
$cpassword=trim($_POST['cpass']);
if($password !== $cpassword) {
//echo "<script> window.location.assign('../errors/password_error.php'); </script>";
header('Location: ../errors/password_error.php');
}
else{
$stmt = $db->prepare('SELECT username FROM utilizador');
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
if($row['username'] === $username){
header('Location: ../errors/username_error.php');
}
}
$img = rand(0,10);
$stmt = $db->prepare("INSERT INTO utilizador (username,nome,password,img) VALUES (:username,:nome,:password,:img)");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':nome', $nome);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':img', $img);
if($stmt->execute()){
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
$_SESSION['img'] = $img;
header('Location: ../main.php');
}
}
}
if(isset($_POST['submit'])){
NewUser();
}
?>
then I changed it to this (got rid of the function) and it worked!
<?php
include_once('../database/connection.php'); // connects to the database
session_start(); // starts the session
if(isset($_POST['submit'])){
global $db;
$username=trim($_POST['user']);
$nome=trim($_POST['nome']);
$password=trim($_POST['pass']);
$cpassword=trim($_POST['cpass']);
if($password !== $cpassword) {
//echo "<script> window.location.assign('../errors/password_error.php'); </script>";
header('Location: ../errors/password_error.php');
}
else{
$stmt = $db->prepare('SELECT username FROM utilizador');
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
if($row['username'] === $username){
header('Location: ../errors/username_error.php');
}
}
$img = rand(0,10);
$stmt = $db->prepare("INSERT INTO utilizador (username,nome,password,img) VALUES (:username,:nome,:password,:img)");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':nome', $nome);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':img', $img);
if($stmt->execute()){
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
$_SESSION['img'] = $img;
header('Location: ../main.php');
}
}
}
?>
Upvotes: 2
Views: 105
Reputation: 458
Clue 1 : Is the password from the database is encrypted ? If yes, you should cypher the input password before making any comparison with it.
Clue 2 :
You could use the same technique to find your username and password. You could do something like this.
$stmt = $db->prepare('SELECT username, password FROM utilizador');
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
if($row['username'] === $username){
header('Location: ../errors/username_error.php');
}
if($row['password'] === $password){
header('Location: ../errors/password_error.php');
}
}
Clue 3 : Have you tried to clear the browser cache inside register.php ?
<?php
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
Upvotes: 0
Reputation: 644
try:
if(strcmp($password,$cpassword)!=0)
instead of:
if(strcmp($password,$cpassword)!==0)
i cant guarantee the working since im kinda new to php
Upvotes: 1