Password and redirect

2

I'm trying to create a site that has a page, where the user needs to enter a password, after entering the password, if it correctly redirects the user to a home page, otherwise it displays an error message, I'm not exactly sure what is going wrong, since when I click on the button I'm just redirected to the php page that appears right after the error msg.

Form index.html:

<form id="formPassword" action="php/verificar.php" method="POST">

                    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 

                    <label for="password">Password:</label>

                    <input id='password' type='password' name="password" size='15' maxlength='15' onfocus="value=''"/>

                    <br>
                    <br>

                    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp

                    <input id='button' src='images/enter_button.png' onmouseover="this.src='images/enter_button_hover.png'" onmouseout="this.src='images/enter_button.png'" alt='Enter' type='image' width='150px' height='30px'/>

                </form>

php page:

<!DOCTYPE html>
<html lang="en">
    <head>

        <title>

        </title>

    </head>

    <body>

        <?php

            $password = $_POST['password'];

            if($password=="senha"){
                include("home.php"); 
            }else{
                echo "<p>Username or Password not entered correctly please try again.</p>"; 
            }

        ?>

    </body>

</html>
    
asked by anonymous 24.08.2015 / 19:45

4 answers

2

Try to put the type of your button there in the form as submit. ;)

    
24.08.2015 / 21:05
2

Put your check to the page you're on. Example:

index.php :

<!-- Trabalhe sempre com arquivos .php para poder executar as condições-->
<form id="formPassword" action="index.php" method="POST">
    <label for="password">Password:</label>
    <input id="password" type='password' name="password" size="15" maxlength='15' onfocus="value=""/>

    <!-- Seu botão deve ser do tipo: submit e NÃO IMAGE e use SEMPRE aspas duplas para os atributos do input -->
    <input id="button" src="images/enter_button.png" onmouseover="this.src=images/enter_button_hover.png" onmouseout="this.src=images/enter_button.png" alt="Enter" type="submit" width="150px" height="30px"/>
</form>

<?php

/*Logo abaixo crie sua condição que receberá a requisição via POST e validará a senha, conforme você mesmo fez: */ 

   //Se houver uma requisição do tipe POST com um valor para password
  if(isset($_POST['password'])){
        $password = $_POST['password'];

        if($password=="senha"){
       //Aqui faça um redirecionamento, uma vez que a senha está correta.
            header('location: home.php'); 
        }else{
            echo "<p>Senha incorreta! Tente novamente.</p>"; 
        }
  }
?>
    
25.08.2015 / 01:45
1

Only coding what Jefferson responded to would be like this:

<input id='submit' src='images/enter_button.png' onmouseover="this.src='images/enter_button_hover.png'" onmouseout="this.src='images/enter_button.png'" alt='Enter' type='image' width='150px' height='30px'/>
    
24.08.2015 / 21:37
1

On your button put the type as submit

<input id='submit' src='images/enter_button.png' onmouseover="this.src='images/enter_button_hover.png'" onmouseout="this.src='images/enter_button.png'" alt='Enter' type='submit' width='150px' height='30px'/>

In the file validar.php instead of using include("home.php"); use header('location: home.php');

When you use include it is as if you were loading the file home.php into the validar.php page itself and that is why the user is not redirected, already using header('location: nomeDaSuaPagina') means that you are modifying the header of the% request HTTP and changing the location of the page to your page in question.

If you want to display a message of login e/ou senha invalidos in the form itself, go to your validar.php file and in the condition else put:

$msg = 'Login ou senha incorretos';
header('location: index.php');  // Troque o index para o nome da sua pagina de login

Just before the login form enter:

<?php if (isset($msg)) { echo $msg }; ?>
// Este trecho verifica se a variavel $msg contem algum valor e caso contenha exibi o valor armazenado nela.
    
24.08.2015 / 22:19