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

天大地大妈咪最大 提交于 2019-12-23 02:41:33

问题


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?


回答1:


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



回答2:


// 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



来源:https://stackoverflow.com/questions/29725387/after-using-a-form-post-how-can-i-store-the-variable-in-session

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