jord49
jord49

Reputation: 582

After using a form POST how can I store the variable in session?

Is this possible? I have sent a value using a form post and retrived in the php but if i refresh it disappears. Can this be stored?

Upvotes: 1

Views: 11113

Answers (2)

DJ MHA
DJ MHA

Reputation: 598

Yes you can store it in SESSION. Please read the following code:-

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Check Post variables are available
if(isset($_POST['username']))
{
    echo $_POST['username']." Username found in form <br />";
    // Set session variables
    $_SESSION["username"] = $_POST['username'];
    echo $_SESSION["username"]." stored in session <br />";;
}
else
    echo 'No, form submitted. Your old stored username was '.$_SESSION["username"];
    //echo 'No, form submitted.';
?>

</body>
</html>

To start session in wordpress

Write below code in your functions.php

function register_my_session()
{
    if( !session_id() )
    {
        session_start();
    }
}

add_action('init', 'register_my_session');

Upvotes: 6

Oli Soproni B.
Oli Soproni B.

Reputation: 2800

// set session to start
/*session is started if you don't write this line can't use $_Session  global variable*/
session_start();


$_SESSION["newsession"]= $value;
$_SESSION['post_session'] = $_POST;

you can see documentation of session http://php.net/manual/en/reserved.variables.session.php

Upvotes: 1

Related Questions