Session not being updated

Asked

Viewed 36 times

-3

To Session does not want to accept the method value POST. She saves the first time, but after the page is updated she 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'] = "";
}?>
  • When you enter the first If, Session is not startled

  • I put but messmo so n worked

  • Pq vc assigns the value of Session to the post? $_post['email'] = $_Session['b']

  • Remove all the session_start(); and add session_start(); only once in the second line of your code (right after the first <?php)

1 answer

0


For use of sessions, you must call the function session_start() 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 function session_start() Start a new session or enable an existing one.

More info: http://php.net/manual/en/function.session-start.php

More info on the global $_SESSION: http://php.net/manual/en/reserved.variables.session.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.