Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!