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

走远了吗. 提交于 2019-12-07 04:37:28

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');
// 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!