问题
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