PHP session_start () only logs in the second time

0

I created a PHP login system that works, but for some reason it only logs in a second time, I tried everything and nothing solved, thank you very much if anyone can help me.

  

below is my login.php

<?php
if (!isset($_SESSION)){
    session_start();
}
require('connect.php');

if (isset($_POST['username']) and isset($_POST['password'])){

    $username = $_POST['username'];
    $password = $_POST['password'];
    $query = "SELECT * FROM 'user' WHERE username='$username' and password='$password'";

    $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
    $count = mysqli_num_rows($result);

    if ($count == 1){
    $_SESSION['username'] = $username;
    }else{

    ?>
    <script>alert("Nome de usuario ou e-mail, inexistentes por favor tente novamente ...");</script>
    <?php
    }
}

if (isset($_SESSION['username'])){
?>

<script>
location.href="dashboard.php"
</script>

<?php }else{ ?>

<div class="container-left-loginregister">

</div>

<div class="container-rigth-loginregister">

    <img src="img2.png">

    <div class="links">
        <a class="btn-loginregister btn-act" href="login.php">Login</a>
        <a class="btn-loginregister" href="register.php">Cadastrar</a>
        <p></p>
    </div>

      <form class="form-signin" method="POST">
        <div class="input-group">
      <span class="input-group-addon"><i class="fa fa-user"></i></span>
      <input type="text" name="username" class="form-control" placeholder="Nome de usuário" required>
    </div>
      <span class="input-group-addon"><i class="fa fa-key"></i></span>
        <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Senha" required>
        <button class="btn btn-lg btn-primary btn-enviar" type="submit">Enviar</button>
      </form>

</div>

<?php } ?>
  

And this is the file to be redirected to and receive the variable dashboard.php

<?php

if (!isset($_SESSION)){
    session_start();
}

require('connect.php');

if (isset($_SESSION['username'])){ 
$username = $_SESSION['username'];

include 'menus.php';
?>

    <header>
        <h1>Painel dashboard</h1>
        <h4><?php echo $username; ?> / Dashboard</h4>
    </header>

    <div class="box-dashboard">

        <h1>Bem vindo(a) a Invest Amigo!</h1>

        <p>Aqui voce pode adicionar seus produtos, ofertas e relatorio para termos em nossa base e investidores poderem saber sobre seu rendimento ...</p>

        <a href="produtos.php">Adicionar um produto</a>, <a href="ofertas.php">Adicionar oferta</a> ou <a href="relatorio.php">relatorio</a>
    </div>

<?php }else{ ?>     

<script>
location.href="login.php"
</script>

<?php } ?>
    
asked by anonymous 18.11.2018 / 16:46

0 answers