How to redirect page after login in php?

Asked

Viewed 7,594 times

4

Hello I would like to know how could redirect page after login of the user. It has the Index that is the code below and here comes other pages like . .php requests, booking.php and others only all are linked in.php requests .

then I would like to know how to redirect after logging in for.php requests

this is my login index

<div id="cadastro">
    <form method="post" action="?go=logar">
        <table id="login_table">
            <tr>
                <td>Usuário:</td>
                <td><input type="text" name="usuario" id="usuario" class="txt" maxlength="15" /></td>
            </tr>
            <tr>
                <td>Senha:</td>
                <td><input type="password" name="senha" id="senha" class="txt" maxlength="15" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Entrar" class="btn" id="btnEntrar"> 
                    &nbsp;<a href="cadastro.php"><input type="button" value="Cadastre-se" class="btn" id="btnCad"></a></td>
            </tr>   
        </table>
    </form>
</div>

</body>
</html>

<?php
if(@$_GET['go'] == 'logar'){
    $user = $_POST['usuario'];
    $pwd = $_POST['senha'];

    if(empty($user)){
        echo "<script>alert('Preencha todos os campos para logar-se.'); history.back();</script>";
    }elseif(empty($pwd)){
        echo "<script>alert('Preencha todos os campos para logar-se.'); history.back();</script>";
    }else{
        $query1 = mysqli_num_rows(mysqli_query("SELECT * FROM USUARIO WHERE USUARIO = '$user' AND SENHA = '$pwd'"));
        if($query1 == 1){
            echo "<script>alert('Usuário logado com sucesso.');</script>"; 
            echo "<meta http-equiv='refresh' content='0, url=./_painel/'>";
        }else{
            echo "<script>alert('Usuário e senha não correspondem.'); history.back();</script>";
        }
    }
}

?>

2 answers

4

It’s simple, you just need to include this line in php:

<?php header('Location:pedidos.php');?>

I hope I’ve helped!

  • I put in index header('Location:pedidos.php'); but I have a registration screen and when I register it already goes to the system without going through the index that is login .

  • 1

    puts it inside if($Query1 == 1){, because if you put it before it already goes straight

  • @allanaraujo, I did not understand what you meant by this "but I have a registration screen and when you register it already goes to the system without going through the index that is login ". Do you want this to happen? Like when he registers, he already goes to the system?

  • I do not have the registration screen ai the user of register and soon after have the login and when the user logs in he is redirected to the system

  • Oh yes, I thought you understood that you should have added this line after login was authenticated...

2


You can do it like this:

$query1 = mysqli_num_rows(mysqli_query("SELECT * FROM USUARIO WHERE USUARIO = '$user' AND SENHA = '$pwd'"));
        if($query1 == 1){
            header("Location: pedidos.php");
        }else{
            echo "<script>alert('Usuário e senha não correspondem.'); history.back();</script>";
        }

or

$query1 = mysqli_num_rows(mysqli_query("SELECT * FROM USUARIO WHERE USUARIO = '$user' AND SENHA = '$pwd'"));
        if($query1 == 1){
            echo "<script>location.href='pedidos.php'</script>";
        }else{
            echo "<script>alert('Usuário e senha não correspondem.'); history.back();</script>";
        }

I hope it helps

Browser other questions tagged

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