On my website I start a session at the top of the page. Later in the page I have included the navigation bar, this is the complete file (nav.php):
<div class="inner">
<ul id="nav" class="menu">
<li class="first"><a href="#">Home</a></li>
<li><a href="#">Brugklas</a>
<ul>
<li class="child"><a href="#">123</a></li>
<li class="child"><a href="#">456</a></li>
<li class="child"><a href="#">789</a>
<ul>
<li><a href="#">Hello</a></li>
<li><a href="#">I’m here!</a>
<ul>
<li><a href="#">Hello</a></li>
<li><a href="#">I’m here!</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Klas 2</a></li>
<li><a href="#">T&O</a></li>
<li><a href="#">Algemeen</a></li>
<?php
if(isset($_SESSION['logged_in'])){
echo '<li><a href="#">Portal</a></li>';
echo '<li class="last"><a href="javascript:void(0);" onclick="logout();">Logout</a></li>';
} else {
echo '<li class="last"><a href="javascript:void(0);" onclick="showLoginScreen();">Login</a></li>';
}
?>
</ul>
Now this works fine, until I want to login. (completely done in jQuery, AJAX-style) As soon as I login successfully. I reload the navigation bar (via AJAX), since it needs to change with updated information (logout link and portal link.) That does not work. As soon as I add session_start(); at the top of nav.php it works, but I get the PHP error that a session already has been started.
I can resolve this by not showing the error. This is done with @session_start(); but in my eyes, the code is still bad. How can I resolve my issue without the @ in front of session_start();
I hope my question is clear.