-2

I am trying to conditionally create a fieldset in the top right of my website that will either prompt the user to login, or show their username and give them the option to logout.

I am using embedded php within my html document in order to achieve this. Unfortunately, both fieldsets are being displayed on my webpage. Can someone please explain to me what I am doing wrong? Thank you!

    <div id="loginForm">

            <?php 

            session_start();


            if(isset($_SESSION['username'])){ ?>

                <form action="endsession.php" method="post">
                <fieldset width="100">
                <legend>Welcome</legend>
                <h3></h3>

                <input type="submit" value="Logout"></input>
                </fieldset>
                </form>

            <?php } else{ ?>

                <form action="connection.php" method="post">
                    <fieldset width="100">
                    <legend>Login</legend>
                    <input type="hidden" name="submitted" id="submitted" value="1"/>

                    <label for="username">Username:</label>
                    <input id="username" name="username" type="text" ><br><br>

                    <label for="password">Password:</label> 
                    <input id="password" name="password" type="password"/><br><br>

                    <input type="submit" value="Sign in"></input>
                    </fieldset>
                    </form>

            <?php } ?>

        </div>
Brendan
  • 11
  • 6
  • 5
    Is this a `.php` file? – chris85 Apr 07 '16 at 23:19
  • 1
    Look at the source code of the page in your browser? Do you see the PHP tags? Then the page is not parsed as PHP, and the code does not affect the result. – Sven Apr 07 '16 at 23:20

1 Answers1

0

Put session_start(); at the top of your page before any other code; just after PHP start tag

<?php
session_start();
<div id="loginForm">
....
and so on...

Hope it fixes the problem.

arshovon
  • 13,270
  • 9
  • 51
  • 69