Password and redirection

Asked

Viewed 290 times

3

I am trying to create a website that has a page, where the user needs to enter a password, after entering the password, if correct it redirects the user to a home page, otherwise it displays an error message, I do not know exactly what is going wrong, since when I click the button I just get redirected to the php page that appears soon to msg error.

Index form.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>
  • here the html code:

4 answers

3

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

3

Put your check to the page you are 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>"; 
        }
  }
?>

2

Only by coding what Jefferson answered would that be:

<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'/>

2

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 archive validar.php rather than using include("home.php"); use header('location: home.php');

when you use include it’s like Voce is loading the file home.php on the page itself validar.php and that is why the user is not redirected, already using header('location: nomeDaSuaPagina') means you are modifying the request header HTTP and changing the location of the page to its page in question.

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

$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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.