[英]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. 我已经使用表单发布发送了一个值,并在php中进行了检索,但是如果我刷新,它就会消失。 Can this be stored? 可以存储吗?
Yes you can store it in SESSION
. 是的,您可以将其存储在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
在wordpress
开始会话
Write below code in your functions.php 在您的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 您可以查看会话的文档http://php.net/manual/zh/reserved.variables.session.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.