empty() check not validating after POST

Jay Gould

I'm setting a session variable in my header.php here:

<?php

session_start();

if(empty($_SESSION['name'])) {
    ?>
    No name set! <br>
    <form action="" method="post">
        <input type="text" name="textName"/>
        <input type="submit" name="submitName" value="Log in"/>
    </form>

    <?php
    if(isset($_POST['submitName'])) {
        $_SESSION['name'] = $_POST['textName'];

    }
}

and validating that the name variable is set on my index here:

<?php

include 'header.php';

if(!empty($_SESSION['name'])){
    ?>
<a href="session_kill.php">Log out</a>
<?php
}

?>

I can confirm that the session variable exists because the logout link appears.

After submitting a name via the form, the session variable updates but the empty() check doesn't reoccur, meaning the textarea stays on the page unless I refresh.

Can anyone tell me why this happens?

Kinshuk Lahiri

Take this part:

if(isset($_POST['submitName'])) {
    $_SESSION['name'] = $_POST['textName'];

}

above this:

if(empty($_SESSION['name'])) {}

When you submit the form, at the beginning; since the session is empty and hence the form remains there and then the session is initialized. And when you refresh the page the session check works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Knockout not validating visible check after startup

From Dev

Jekyll: check if post content empty

From Dev

Check whether POST array is empty

From Dev

$_POST empty after form submit

From Dev

$_POST is empty after simple AJAX POST

From Dev

Validating TinyMCE for empty inputs

From Dev

Validating empty $_FILES

From Dev

Validating TinyMCE for empty inputs

From Dev

Check $_POST array have empty values

From Dev

AJAX Username check not validating

From Dev

Validating check boxes in HTML

From Dev

Validating POST values

From Dev

How to check if LinqDataSource is empty after selecting?

From Dev

$_post remains empty after form submission

From Dev

$_POST and $_FILES empty after AJAX file upload

From Dev

Empty fields after POST with HTML / PHP?

From Dev

PHP empty $_Files after Form POST

From Dev

variable undefined after isset check, empty check and initalization

From Dev

$_POST array empty in PHP after an a request with post data

From Dev

Check if a post was posted after a certain date

From Dev

after curl post, how to check the postfield - to debug

From Dev

Validating a ListBox's contents are not empty

From Dev

Validating a ListBox's contents are not empty

From Dev

Jquery validating not empty and is URL in forms

From Dev

Function to Check if POST is empty and add custom error messages

From Dev

How to check for empty JSON field in flask reqparse for a post request?

From Dev

Function to Check if POST is empty and add custom error messages

From Dev

Validating and then redirecting to a post route in Laravel

From Dev

Form model not validating before POST