How to Block navbar functions only for logged in users?

1

I would like to know how to block part of this navbar , with the session, only for users logged into the page

NavBar:

<header class="masthead">
    <nav class="navbar navbar-expand-md navbar-light bg-light rounded mb-3">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav text-md-center nav-justified w-100">
          <li class="nav-item active">
            <a class="nav-link" href="/index.php">Inicio <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/videos.php">Videos</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/downloads.php">Downloads</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#" target="_blank">Forum</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">TeamSpeak</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Usuario</a>
            <div class="dropdown-menu" aria-labelledby="dropdown01">
              <a class="dropdown-item" href="/login.php">Login</a>
              <a class="dropdown-item" href="/cadastrar.php">Register</a>
            </div>
          </li>
        </ul>
      </div>
    </nav>

Validation Code Login

<?php
    session_start();
    include_once("conexao.php");
    $btnLogin = filter_input(INPUT_POST, 'btnLogin', FILTER_SANITIZE_STRING);
    if($btnLogin){
        $usuario = filter_input(INPUT_POST, 'usuario', FILTER_SANITIZE_STRING);
        $senha = filter_input(INPUT_POST, 'senha', FILTER_SANITIZE_STRING);
        //echo "$usuario - $senha";
        if((!empty($usuario)) AND (!empty($senha))){
            //Gerar a senha criptografa
            //echo password_hash($senha, PASSWORD_DEFAULT);
            //Pesquisar o usuário no BD
            $result_usuario = "SELECT id, nome, email, senha FROM usuarios WHERE usuario='$usuario' LIMIT 1";
            $resultado_usuario = mysqli_query($conn, $result_usuario);
            if($resultado_usuario){
                $row_usuario = mysqli_fetch_assoc($resultado_usuario);
                if(password_verify($senha, $row_usuario['senha'])){
                    $_SESSION['id'] = $row_usuario['id'];
                    $_SESSION['nome'] = $row_usuario['nome'];
                    $_SESSION['email'] = $row_usuario['email'];
                    header("Location: site.php");
                    die;
                }else{
                    $_SESSION['msg'] = "Login e senha incorreto!";
                    header("Location: /login.php");
                }
            }
        }else{
            $_SESSION['msg'] = "Login e senha incorreto!";
            header("Location: /login.php");
        }
    }else{
        $_SESSION['msg'] = "Página não encontrada";
        header("Location: /login.php");
    }

Session I tried to create to block

<?php
    session_start();
    if(!empty($_SESSION['id'])){
        echo "Olá ".$_SESSION['nome'].", Bem vindo <br>";
        echo "<a href='sair.php'>Sair</a>";
    }else{
        $_SESSION['msg'] = "Área restrita";
        header("Location: /login.php"); 
        session_destroy();
    }
    
asked by anonymous 08.12.2017 / 23:12

1 answer

0

You can add a check inside your html code. For each link that you want to block for logged in users you add the following code:

<?php
    session_start();
    if (isset($_SESSION['id'])) {
        echo '<li class="nav-item active"><a class="nav-link" href="/index.php">Inicio <span class="sr-only">(current)</span></a></li>';
    }
?>
    
09.12.2017 / 13:54