The session is not being updated

-3

Session does not want to accept method value POST . It saves the first time, but after the page is updated it disappears.

<?php
if(isset($_SESSION['b'])){   
    if ($_SESSION['b'] !=""){

        $_POST['email'] =$_SESSION['b'];

        echo $_SESSION['b']."<br/>1";
        }else{

            $_SESSION['b'] = $_POST['email'];
            echo$_SESSION['b']."<br/>2";
        }
}
else{
    session_start();
    $_SESSION['b'] = "";
}?>
<form name="form" action="" method="POST">
Email: <input type="email" name="email" class="email" id="email">
                </div>
                <input type="submit" name="enviar" id="enviar" class="enviar" value="Encurtar" />
</form>
<?php
if(isset($_SESSION['b'])){   
    if ($_SESSION['b'] !=""){

        $_POST['email'] =$_SESSION['b'];

        echo $_SESSION['b']."<br/>1";
        }else{

            $_SESSION['b'] = $_POST['email'];
            echo$_SESSION['b']."<br/>2";
        }
}
else{
    session_start();
    $_SESSION['b'] = "";
}?>
    
asked by anonymous 26.03.2018 / 00:07

1 answer

0

To use sessions, you must call the session_start () function before calling the global variable $ _ SESSION . Another point is that there is no need to call this function twice on the same php page.

In your case, it would look like this:

<?php
    session_start();

    if(isset($_SESSION['b']))
    {
        $_POST['email'] = $_SESSION['b'];
        echo $_SESSION['b']."<br/>1";
    }
    else
    {
        $_SESSION['b'] = "";
    }
    ...

The session_start () function starts a new session or enables an existing session.

More info: link

More info on global $ _SESSION: link

    
26.03.2018 / 16:23