会话php语句

发布于 2024-12-11 09:13:16 字数 319 浏览 0 评论 0原文

我如何将其命令放入一个语句中,该语句将所有 $_POST[] 放入会话中,而不必写出每一行。

<?php session_start(); 
$_SESSION['s_type'] = $_POST['s_type'];
$_SESSION['s_uname'] = $_POST['s_uname'];
$_SESSION['s_email'] = $_POST['s_email'];
$_SESSION['s_promo'] = $_POST['s_promo'];
$_SESSION['s_ctry'] = $_POST['s_ctry'];
?> 

how can i command this into a statement that will just put all the $_POST[] into a session without have to write each line out.

<?php session_start(); 
$_SESSION['s_type'] = $_POST['s_type'];
$_SESSION['s_uname'] = $_POST['s_uname'];
$_SESSION['s_email'] = $_POST['s_email'];
$_SESSION['s_promo'] = $_POST['s_promo'];
$_SESSION['s_ctry'] = $_POST['s_ctry'];
?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假扮的天使 2024-12-18 09:13:16
$_SESSION['POST'] = $_POST;
echo $_SESSION['POST']['s_type'];
$_SESSION['POST'] = $_POST;
echo $_SESSION['POST']['s_type'];
怪我鬧 2024-12-18 09:13:16

如果你不喜欢Marc B的嵌套,

foreach ($_POST as $k => $v){
    $_SESSION[$k] = $v;
}

If you don't like the nesting of Marc B,

foreach ($_POST as $k => $v){
    $_SESSION[$k] = $v;
}
二手情话 2024-12-18 09:13:16

如果您有理由选择哪些 $_POST 值进入会话存储,哪些不进入会话存储(例如,如果您有每次都必须重新输入的验证码,或者信用卡号或敏感信息),创建一个 $_POST 键数组来存储和迭代它:

// We won't store s_email in $_SESSION. All others stored
$store_to_session = array('s_stype','s_uname','s_promo','s_city');
foreach ($store_to_session as $s) {
  $_SESSION[$s] = $_POST[$s];
}

If you have a reason to be selective about which $_POST values go to session storage and which don't (for example, if you had a CAPTCHA that had to be re-entered each time, or a credit card number or something sensitive), make an array of the $_POST keys to store and iterate over it:

// We won't store s_email in $_SESSION. All others stored
$store_to_session = array('s_stype','s_uname','s_promo','s_city');
foreach ($store_to_session as $s) {
  $_SESSION[$s] = $_POST[$s];
}
星星的轨迹 2024-12-18 09:13:16
$_SESSION = $_POST;

将创建 $_POST 数组的副本并将其分配给 $_SESSION,这将完成您所要求的任务。但这也会清除 $_SESSION 数组中的所有数组成员。所以我推荐一个循环,它将维护 $_SESSION 中已有的数组成员。

foreach ($_POST as $key=> $val){
    $_SESSION[$key] = $val;
}
$_SESSION = $_POST;

Would create a copy of the $_POST array and assign it to $_SESSION, which accomplishes what you are asking. But this will also wipe out any array members in the $_SESSION array. So I recommend a loop, which will maintain the array members already in $_SESSION.

foreach ($_POST as $key=> $val){
    $_SESSION[$key] = $val;
}
心病无药医 2024-12-18 09:13:16

试试这个;)

不要直接将 $_POST 分配给 $_SESSION,而是创建一个像这样的嵌套分数:

$_SESSION['putScopeNameHere'] = $_POST;

在这种情况下,您的 session 数据也是post 数据分配后即可使用。

Try this ;)

Instead of direct assigning $_POST into $_SESSION create a nested score like this:

$_SESSION['putScopeNameHere'] = $_POST;

In this case, your session data is also available for you after post data assignment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文