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

Asked

Viewed 258 times

0

I am making a page, using PHP I entered an access level to it. The access levels are user and admin. Just that I want when the access level is admin a button appears 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 when the access level is admin the following element appears:

 <div>
     <a href="index.html">
          <img id="back-arrow" src="imagem/back-arrow.png" alt="voltar">
     </a>
 </div>
  • But haven’t you already done this in your code? Are you making a mistake?

  • And why are you making the button appear before HTML?

  • Yes, but appear where? in what place inside your HTML?

2 answers

2

  • Place the second conditional within the first conditional (optional)

  • Create a variable, example $divAdm, the value of which 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 wherever you wish inside the BODY of your HTML <?php echo $divAdm; ?>, example:

    <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 better to create only

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

within the variable $divAdm and place inside 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 then 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>
  • Guy worked really well. I thank you from the bottom of my heart!!!

-1


Enter this code in the location 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 } ?>
  • <?php if($_SESSION['nivel'] == "admin") { ?>&#xA;<div><a href="index.html"><img id="back-arrow" src="imagem/back-arrow.png" alt="voltar"></a></div>&#xA;<?php } ?>

  • I don’t know Why but the same code does not appear on the Answer it self. But I Believe it will Solve your problem.

  • Bro was perfect. Exactly what I wanted!!!

  • Please mark as a valid reply. See how and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-accepta-uma-reply

Browser other questions tagged

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