Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda