1

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&rsquo;m here!</a>
                       <ul>
                            <li><a href="#">Hello</a></li>
                            <li><a href="#">I&rsquo;m here!</a></li>
                        </ul> 
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">Klas 2</a></li>
    <li><a href="#">T&amp;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.

0

1 Answer 1

2

put this at the top of nav.php

if (!isset($_SESSION)) {session_start();}

the problem occurs when you load the page at the beginning. you start the session and you include the nav.php where you start the session once again.

if you load the page for the second time the page is independent of the parent and the session can be started again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.