How do I display HTML and CSS elements in PHP in a specific access level

1

I'm doing a page, using PHP I've entered a level of access to it. Access levels are user and admin. But I want that when the level of access for admin appears a button and when it is user does not appear the same

<?php
session_start();
if($_SESSION['nivel'] == "usuario" || $_SESSION['nivel'] == "admin")  
{

}
if($_SESSION['nivel'] == "admin")
{
    echo '<div class="container" >';
    echo '<a href="index.html"><img id="back-arrow" src="imagem/back- 
arrow.png" alt="voltar"></a>';
    echo '</div>';

}else{              
    header("Location:index.html");
exit;           
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Estuda+</title>
<link rel="stylesheet" 
href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" 
<link rel="stylesheet" href="css/pato.css">
<link rel="shortcut icon" href="../imagens/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<style>
#sair {
    position: absolute;
    left: 3%;
    margin-left: -35px;
    transition-duration: 0.5s;
    border-radius: 50%;
}
#sair:hover {
    background-color: #428db8;
    box-shadow: -5px 5px black;
}
#back-arrow {
position: absolute;
width: 48px;
height: 48px;
left: 15%;
top: 85%;
margin-left: -35px;
transition-duration: 0.5s;
border-radius: 50%;

}
#back-arrow:hover{
background-color: #428db8;
box-shadow: 5px 5px 2px 2px black;
}
</style>
</head>

<body>
<div class="container" >
<a href="logout.php"><img id="sair" src="imagem/exit.png" alt=""></a>
</div    >
<nav>

<ul class="menu">

    <li>
        <a href="#" title="#" class="borda">Ciências exatas <i id="icone" 
class="fas fa-arrow-down"></i></a>
            <ul>
                <li><a href="../site/matematica/matematica.html" 
target="_blank">Matemática</a></li>
                <li><a href="#" target="_blank">Física</a></li>
                <li><a href="#" target="_blank">Química</a></li>
            </ul>
    </li>
    <li><a class="borda" href="#">Ciências biológicas <i class="fas fa- 
arrow-down"></i></a>
        <ul>
             <li><a href="\Celular Gerentes.html" 
target="_blank">Biologia</a></li>                                            
        </ul>
    </li>
    <li style="background:none; position: relative;">
        <img src="../imagens/logo%20estudamais.png" width="200px" 
class="top" >
    </li>
    <li>
        <a href="#" class="borda" title="Importação de folhas de 
estilo">Linguagens <i class="fas fa-arrow-down"></i></a>
        <ul>
            <li><a href="#" target="_blank">Gramática</a></li>
            <li><a href="#l" target="_blank">Redação</a></li>
            <li><a href="#" target="_blank">Literatura</a></li>
            <li><a href="#" target="_blank">Inglês</a></li> 
            <li><a href="#" target="_blank">Espanhol</a></li> 
        </ul>
    </li>
    <li>
        <a href="#" class="borda" title="Fale conosco">Ciências humanas <i 
class="fas fa-arrow-down"></i></a>
        <ul>
            <li><a href="#l" target="_blank">Geografia</a></li>
            <li><a href="#" target="_blank">História</a></li>
            <li><a href="#" target="_blank">Sociologia</a></li> 
            <li><a href="#" target="_blank">Filosofia</a></li> 

        </ul>
    </li>
</ul>
</nav>
<div id="banner" style="width:100%" style="height:100%"><img 
src="../imagens/banner-escola.jpg" style="width:100%" style="height:100%"> 
</div>        
</body>
</html>

I want the following element to appear when the access level is admin:

 <div>
     <a href="index.html">
          <img id="back-arrow" src="imagem/back-arrow.png" alt="voltar">
     </a>
 </div>
    
asked by anonymous 03.11.2018 / 03:01

2 answers

2
  • Put the second conditional within the first conditional (optional)

  • Create a variable, example $divAdm , whose value is the container <div class="container"> .....</div>

    <?php
    session_start();
    if($_SESSION['nivel'] == "usuario" || $_SESSION['nivel'] == "admin")  
    {
    
        if($_SESSION['nivel'] == "admin")
        {
           $divAdm = '<div class="container">
             <a href="index.html"><img id="back-arrow" src="imagem/back- 
             arrow.png" alt="voltar"></a>
             </div>';
    
        }else{              
           header("Location:index.html");
        exit;           
        }
    
    }
    ?>
    
  • display it where you want it within BODY of your HTML <?php echo $divAdm; ?> , eg:

    <body>
    <div class="container" >
    <a href="logout.php"><img id="sair" src="imagem/exit.png" alt=""></a>
    </div    >
    <?php echo $divAdm; ?>
    
  

I believe it would be best to create only

<a href="index.html"><img id="back-arrow" src="imagem/back- 
                 arrow.png" alt="voltar"></a>
  

within the variable $divAdm and put within the existing <div class="container" > in your HTML

    <body>
    <div class="container" >
      <a href="logout.php"><img id="sair" src="imagem/exit.png" alt=""></a>
      <?php echo $divAdm; ?>
    </div>

The result would be:

<body>
<div class="container" >
  <a href="logout.php"><img id="sair" src="imagem/exit.png" alt=""></a>
  <a href="index.html"><img id="back-arrow" src="imagem/back- 
                 arrow.png" alt="voltar"></a>
</div>
    
03.11.2018 / 12:53
-1

Enter this code wherever the button appears

<?php if($_SESSION['nivel'] == "admin") { ?>
<div><a href="index.html"><img id="back-arrow" src="imagem/back-arrow.png" alt="voltar"></a></div>
<?php } ?>
    
03.11.2018 / 04:11