S. Shrestha S. Shrestha - 20 days ago 5
PHP Question

How to solve undefine index?

How to solve undefined index although i have started session in the top of the header file it shows undefined index?


The following error was shown:-
Notice: Undefined index: signed_in in C:\xampp\htdocs\web_forum\header.php on line 26


line 26

if($_SESSION['signed_in'])


header.php



<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="description" content="A short description."/>
<meta name="keywords" content="put, keywords, here"/>
<title>Webinar</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h1>Webinar</h1>

<div id="wrapper">
<div id="menu">
<a class="item" href="index.php">Home</a> -
<a class="item" href="create_topic.php">Create a topic</a> -
<a class="item" href="create_cat.php">Create a category</a>



<div id="userbar">
<?php
if($_SESSION['signed_in'])
{
echo 'Hello <b>' . htmlentities($_SESSION['user_name']) . '</b>. Not you? <a class="item" href="signout.php">Sign out</a>';
}
else
{
echo '<a class="item" href="signin.php">Sign in</a> or <a class="item" href="signup.php">create an account</a>';
}
?>
</div>

</div>
<div id="content">





footer.php



</div><!--content-->

</div><!--wrapper-->

<div id="footer">Created for webinar by Swastik Shrestha.</div>

</body>
</html>

Answer

You should use isset() method like this:

if(isset($_SESSION['signed_in'])) { ... }

Hope this helps!