I am trying to save my _POST array to a _SESSION array so that I can use that in other pages of the website
when i do
$_SESSION = $_POST;
it didn’t worked.
Also the following code also is giving a error and not copying it.
foreach($_POST as $element){
$_SESSION[] = $element;
}
$_SESSIONcan’t handle numeric keys; it must be an associative array. I.e. if you dolike you do in your foreach-loop, it’ll create a new numeric key like in any other array (e.g.
$_SESSION[0] == "foo"), but PHP will skip the key when saving the session. You get an “notice”-level warning like “Skipping numeric key 0”. So the next time the$_SESSIONarray is read, it won’t contain any numeric keys.So you must use a string index, like:
That should work.
You should also be able to do
$_SESSION = $_POSTand overwrite the entire$_SESSIONarray. Can’t say why that doesn’t work. But I can’t see why you would want to do it either. If you ever want to use$_SESSIONfor anything else (like, actual session data), you can’t have your code randomly overwriting the entire array with post data here and there. Better to just use a string index like above to store the post data.