Help when logging in login system, disappear the 2 forms

Asked

Viewed 67 times

0

Well I am wanting to leave a login system that after being validated the login when it appears to the page of the login form that it is logged in and not the login form: ex: inserir a descrição da imagem aqui

we log in the same page if this form is missing: inserir a descrição da imagem aqui

php code:

<?php
    session_start();

    require 'init.php';
?>
<html>
<head>
<meta charset="utf-8">
<title>x1</title>
<link rel="stylesheet" href="css/css.css">
</head>

<body>

<header>
<div id="logobuscar">
<div id="wrapperlogo">
<div id="logo"><img src="images/logosecure.png" alt="SecureStore" height="120" width="120"></div>
<div id="buscar">   <?php
                $sessao = $_SESSION['pedido'];
                $consulta = $pdo->prepare("SELECT * FROM carrinho_temporario WHERE temporario_sessao =:ses");
                $consulta -> bindValue(':ses', $sessao);
                $consulta -> execute();
                $linhas = $consulta -> rowCount();
                ?>
                <p class="text-right "><a href="carrinho.php" class="color-white bgcolor-red font-text-light font-weight-heavy car_show">Carrinho(<?= $linhas ?>)</a></p><br>
                <h1 class="color-white text-center font-text-hard-two font-weight-heavy link-bgcolor-black">PRODUTOS</h1>
</form></div>
</div>
</div>  
<div id="mainmenu">
<div id="wrappermenu">
<nav class="menu">
<ul class="x1">
<a href="#"><li>Inicio</li></a>
<a href="#"><li>Sobre</li></a>
<a href="#"><li>Loja</li></a>
<a href="#"><li>Contato</li></a>
</ul>

<ul class="x2">
<a href="#"><li>Cadastrar</li></a>
<a href="logar.php"><li>Login</li></a>
</ul>
</nav>
</div>
</div>
</header>
    <div id="wrapperleft"></div>

    <div id="wrapper">
           <form action="login.php" method="post">
            <label for="email">Email: </label>
            <br>
            <input type="text" name="email" id="email">

            <br><br>

            <label for="password">Senha: </label>
            <br>
            <input type="password" name="password" id="password">

            <br><br>

            <input type="submit" value="Entrar">
        </form>
    </div>

    <div id="wrapperright"></div>

<footer>
</footer>
</body>
</html>

validation code:

<?php

// inclui o arquivo de inicialização
require 'init.php';


// resgata variáveis do formulário
$email = isset($_POST['email']) ? $_POST['email'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';

if (empty($email) || empty($password))
{
    echo "Informe login e senha";
    exit;
}

// cria o hash da senha
$passwordHash = make_hash($password);

$PDO = db_connect();

$sql = "SELECT id, nome, adm FROM users WHERE login = :email AND password = :password";
$stmt = $PDO->prepare($sql);

$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $passwordHash);

$stmt->execute();

$users = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (count($users) <= 0)
{
    echo "login ou senha incorretos";
    exit;
}

// pega o primeiro usuário
$user = $users[0];

session_start();
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['name'];
$dados=mysqli_fetch_assoc($sql);
if($dados['adm'] == '1') header("Location: administrativo.php");
header('Location: logar.php');
  • Can you tell me when he’s logged in?

  • I put it to when log it vote for the same page. log in.php'

  • Only when I hit the login link or the /log.php directory the form still appears.

  • I wanted to do, when the user logs in even he voting to login.php page the form disappear and appear something else ( a panel )

1 answer

2


Just check if you are not logged in to submit the form

<?php
if ($_SESSION['logged_in'] != true){
?>


       <form action="login.php" method="post">
        <label for="email">Email: </label>
        <br>
        <input type="text" name="email" id="email">

        <br><br>

        <label for="password">Senha: </label>
        <br>
        <input type="password" name="password" id="password">

        <br><br>

        <input type="submit" value="Entrar">
    </form>
</div>
<?php
}
?>

another way

create a Session on login $_SESSION['esconder'] = "display: none;";

and in the form tag of the login

<form action="login.php" method="post" style="<?php echo $_SESSION['esconder'] ?>">

you must do this if the login is valid

addendum:

<?php
if ($_SESSION['logged_in'] != true){

    echo '<a href="logar.php"><li>Login</li></a>';

}else{

    echo $_SESSION['user_name'];
}
?>
  • show @leo, mt thanks .

  • @Gabrielcosta, on the Undefined index logged_in , what happens is the following: if you drop this Sesssion does not exist, so in the file log in when asking the question if ($_SESSION['logged_in'] != true){ PHP won’t know how to evaluate because if Session doesn’t exist, how can you compare something that doesn’t exist with some value? then it displays this error. To avoid this warning you can use if (!isset($_SESSION['logged_in'])) { which means se a session não existir. Isset checks whether it has been initialized, i.e., whether it exists, and the point of interjection the negation.

Browser other questions tagged

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