1

I've created a working session (with help from here I might add) and I've managed to get it to store a variable across multiple files without any problems.

When $username isn't filled, there's a prompt for the user to submit their username and upon submitting $username is assigned the value of the user's name and the form is replaced with text, no longer prompting the user to enter a username, in theory.

Here's the code I have right now:

<?php
session_start();

?>

<header>
    <!DOCTYPE html>
    <link rel="stylesheet" type="text/css" href="style/main.css">
    <title>webshop</title>

</header>

<div id="LogIn">
    <?php
    if(isset($_SESSION['username'])){
    echo 'Current session username: '.$_SESSION['username'];
    echo '<br /><a href="logout.php">Destroy current session</a>';
} else {
        ?>
        <form class="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" id="form1">
            <fieldset>
                <ul>
                    <p>Please enter your username to continue to the webshop.</p>
                    <label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name"
                                                                     class="required" role="input"
                                                                     aria-required="true"/></span>

                    <input class="submit transparentButton" value="Next" type="submit" name="Submit"/>
                </ul>
                <br/>
            </fieldset>
        </form>
        <?php

        if (isset($_POST['Submit'])) {
            $_SESSION['username'] = $_POST['username'];
        }

    }
    ?>
</div>


<a href="cart.php">cart</a><br />
<a href="index.php">index</a>

The problem I'm having is that once the user has entered their username into the form and clicks "next", the page reloads and the form is still there. If you then refresh that page, it replaces the form with the text and the session variable $username parsed as plain text with a link to logout (session_destroy()).

My question is why do I have to refresh the page for the session variable to be displayed properly? Is it something to do with the if statement?

Thanks in advance.

1 Answer 1

7

You simply have a logic / ordering problem.

Move this piece of code that is currently below your form:

    if (isset($_POST['Submit'])) {
        $_SESSION['username'] = $_POST['username'];
    }

to the top of your file, just below the session_start(), and it will behave as you intend.

The way your code is written now, the session variable is not set until AFTER the form displays. You want the session variable to be set BEFORE the form displays (if in fact the $_POST username is set).

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

1 Comment

Wow that was easy, thanks a lot! I'll mark as resolved in a few minutes when I can, thanks a lot!

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.