I have this html form:

```html
<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');
        }
    }
}

?>

```