How to Block Navbar Functions for Logged-in Users Only?

Asked

Viewed 215 times

1

I wanted to know how to block part of this navbar, with the session, only for users logged in to 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>

Code of the Login Validator

<?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();
    }
  • 1

    Put an if on the part you block with the isset condition of SESSION if that part does not appear and if it does not exist.

  • I got it solved, obg

1 answer

0

You can add a check inside your html code. For each link 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>';
    }
?>
  • Thanks, I had already solved using <? php if(!Empty($_SESSION['id'])){ ?>

  • @Lucasrodz remembering that if the $_SESSION['id'] variable does not exist an error will appear in your code, then it is better to check if it exists with isset.

Browser other questions tagged

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