PHP - Convert all POST data into SESSION variables
php, post, session-variables
Solution
I would specify a dictionary of POST names that are acceptable.
$accepted = array('foo', 'bar', 'baz');
foreach ( $_POST as $foo=>$bar ) {
if ( in_array( $foo, $accepted ) && !empty($bar) ) {
$_SESSION[$foo] = $bar;
}
}
Or something to that effect. I would not use `empty` because it treats `0` as empty.
Problem
There's got to be a much more elegant way of doing this. How do I convert all non-empty post data to session variables, without specifying each one line by line? Basically, I want to perform the function below for all instances of X that exist in the POST array. ``` if (!empty($_POST['X'])) $_SESSION['X']=$_POST['X']; ``` I was going to do it one by one, but then I figured there must be a much more elegant solution