0

I've got an if statement to check if a variable within the $_SESSION is active and set, and if it is then a message is returned to the user. Here's my header.php:

<?php
    $conn = HIDDEN;

    session_start();
    $username = '';
    $_SESSION['username'] = $username;
    ?>

    <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 "its working";
        } else {
            ?><form class="form1" method="post" action="" 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 } ?>
    </div>

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

    <?php
    if (isset($_POST['Submit'])) {
        $_SESSION['username'] = $_POST['username'];

        // Use the following code to print out the variables.
        echo 'Session: '.$_SESSION['username'];
        echo '<br>';
        echo 'POST: '.$_POST['username'];
    }
?>

The first time running, or before the user logs out (to be implemented later), the site should prompt for a username to be entered and then upon refreshing the page the welcome message should be display.

As of right now the code simply returns "it's working" despite no variable in $username existing. The code:

<?php
if (isset($_POST['Submit'])) {
       $_SESSION['username'] = $_POST['username'];

        // Use the following code to print out the variables.
        echo 'Session: '.$_SESSION['username'];
        echo '<br>';
        echo 'POST: '.$_POST['username'];
    }
?>

should print out the variable underneath the welcome message, or nothing at all if it's empty. As of right now, the welcome message "it's working" is displayed always but no variables are in $username. Can anyone tell me why?

Thanks in advance.

8
  • doesn't look like the session was started elsewhere Commented Nov 26, 2015 at 15:56
  • session_start() is always on the first line. just a quick glance, goodnight Commented Nov 26, 2015 at 15:56
  • Didn't make any different, I moved it to the first line and the same happens. @Fred-ii- could you explain what you mean please? To my knowledge the session is active, I can't see why else I would need to start another one. Commented Nov 26, 2015 at 15:58
  • your last body of code doesn't contain session_start(); in there. If you're using it, please update your question. I deleted my answer. Use error reporting. Commented Nov 26, 2015 at 16:02
  • <?php if (isset($_POST['Submit'])) { $_SESSION['username'] = $_POST['username']; - Where's session_start(); in here? That's what I meant about it. My answer contained it, but I deleted it. Let me know if you want me to undelete it. It was the first answer submitted. Commented Nov 26, 2015 at 16:07

3 Answers 3

1

$_SESSION['username'] is SET/NULL but is EMPTY you should try !empty() instead if isset(). See below.

<?php
if (!empty($_SESSION['username']))
{
    echo "its working";
} else {
    ?><form class="form1" method="post" action="" 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 } ?>

EDIT 2 As to the comment.

IF statement needed to tell if there was a submit if there was don't display the form else display the form. See below Code

<?php
    $conn = ""; //HIDDEN kept throwing error whilst I was testing

    session_start();
    $username = '';
    $_SESSION['username'] = $username;
    ?>

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

    </header>

    <div id="LogIn">
        <?php
            if (isset($_POST['Submit'])) {
                $_SESSION['username'] = $_POST['username'];

                $_SESSION['username'] = $_POST['username'];

                // Use the following code to print out the variables.
                echo 'Session: '.$_SESSION['username'];
                echo '<br>';
                echo 'POST: '.$_POST['username'];

            } else {

        if (!empty($_SESSION['username']))
        {
            echo "its working";
        } 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"/> //removed css selector .
                    </ul>
                    <br/>
                </fieldset>
            </form>
        <?php } } ?>


    </div>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for replying. I've used it and it's worked, mostly. It's now returning the variable $username correctly but still parsing the form instead of the welcome message. I've got the test in the bottom of the OP to see if $username has the correct variable in and it's working, any idea what might be happening?
@JayGould So does the form show now? If you fill out the username field does it return anything?
the form shows and the test (at the bottom of the OP) shows the $username variable with the value given in the form, but the form is constantly visible even though the if statement says if $username isn't empty then don't parse it. The test I included at the bottom show me the variable being set (and the ability to change it too), so I'm really not sure what's happening.
Would it be because I'm declaring $username = ''; at the top by any chance? It's emptying the variable each time header is ran...
@JayGould I have added an EDIT 2 which should only display the form if submit is set.
|
0

The isset() checks only whether the variable is set or not and it returns true since it is initialized to null string. Here you have to use !empty().

if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {

}

Comments

0

Just a quick solution is to change

  if (isset($_SESSION['username']))

to

 if (strlen($_SESSION['username']) > 0)

That will work. Im guessing it because technically u did set username so it isset but if u check the length u know its not empty

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.