acctman
acctman

Reputation: 4349

Session php statement

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'];
?> 

Upvotes: 2

Views: 145

Answers (5)

itzmukeshy7
itzmukeshy7

Reputation: 2677

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.

Upvotes: 0

steampowered
steampowered

Reputation: 12062

$_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;
}

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270637

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];
}

Upvotes: 1

user8710
user8710

Reputation:

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

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

Upvotes: 5

Marc B
Marc B

Reputation: 360702

$_SESSION['POST'] = $_POST;
echo $_SESSION['POST']['s_type'];

Upvotes: 10

Related Questions