Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
How to define index "UserName" in session?

I'm trying to show the login form to visitors only and hide it when user is logged in. I've seen similar question here but I couldn't get the answer. In the current code I get the Login form which is included from another page and also an error " Undefined index: UserName". So how can I define it?

<?php session_start() ?>
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="IndexStyle.css"/>
    <meta name="viewport" content="width= device-width, intial-scale=1.0">

</head>
<body class="body">
    <header class="MainHeader">
    <img src="images/header-image-5.jpg">
    <nav><ul>
         <li class="active"><a href="index.php">Home</a></li>
         <li><a href="Product.php">Products</a></li>
         <li><a href="AboutUs.php">About Us</a></li>
         <li><a href="ContactUs.php">Contact Us</a></li>
         <div id="AdminP">
            <li><a href="AdminPanel.php">Admin Panel</a></li>
         </div>
        </ul>
    </header>
    <div class="MainBody">
        <p>This is Mainbody 
        </p>
    </div>
    <aside class="SideBar">
        <div class="login">
<?php
    if($_SESSION['UserName'] == "") {

        include 'Form.php';
    } else {
        echo "Logged in";
    }
?>
        </div>        
      <p>&nbsp;</p>
    </aside>
    <Footer class="Footer"> 
        <p>Copyrights reserved 
        </p>
    </Footer>
</body>
</html>
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You need to check session right way if($_SESSION['UserName'] == "") {

You can use isset and empty for checking session.

session_start();
if(isset($_SESSION['UserName']) && !empty($_SESSION['UserName']))
{
include 'Form.php';
}
else {
echo "Logged in";
}

you can also use array_key_exists instend of isset

session_start();
if(array_key_exists('UserName',$_SESSION) && !empty($_SESSION['UserName'])) {
include 'Form.php';
}else{
echo "Logged in";
}

Above methods for (especially for array keys) it works and also you can go for

if (!isset($_SESSION['UserName']) || $_SESSION['UserName'] == '')
over 4 years ago · Santiago Trujillo Relatório

0

try this Dear Change your php code

<?php
    if(!(isset($_SESSION['UserName']) && $_SESSION['UserName'] != "")) 
    {
      echo "Logged in";
    } 
    else 
    {
     include 'form.php';
    }
?>
over 4 years ago · Santiago Trujillo Relatório

0

<?php
    if(!isset($_SESSION['UserName']) &&  $_SESSION['UserName']== "") {

        include 'Form.php';
    } else {
        echo "Logged in";
    }
?>

hope this will help you

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda