Session::save($key, $value)
$key
is usually a string key while the value supplied can be of any data type. Once this
method is called, the key will be used to save the data into the global session space.
Session::value()
method is used to obtain the value of a specified key from the global session space. Although it requires a key
to know which value it is required to return, if not key is specified, the entire session content is returned.
Session::value($key?, $subkey?)
where:
$key : session key whose value is expected to be returned
$subkey : a subkey of the value of $key if the value is an array.
$key
above is used to fetch the value of an existing key in the storage. If $subkey
is specified, and the value of $key supplied is an array, then $subkey
is assumed to be a subkey of the value of
$key
. This means that the value of the subkey will be returned instead. An example is shown below:
$data1 = 'foobaz'; $data2 = [ 'name1' => 'foo', 'name2' => 'bar', 'name3' => 'baz', ]; Session::save('data1', $data1); Session::save('data2', $data2); $session_data = Session::value(); // returns both data1 and data2 $session_data1 = Session::value('data1'); // foobaz $session_data2 = Session::value('data2'); // ['name1' => 'foo', 'name2' => 'bar', 'name3' => 'baz'] $session_data1 = Session::value('data2'); // ['name1' => 'foo', 'name2' => 'bar', 'name3' => 'baz'] $session_name1 = Session::value('data2', 'name1'); // foo $session_name2 = Session::value('data2', 'name2'); // bar $session_names = Session::value('data2', ['name1','name3']); // ['foo', 'baz']
Session::value()
works. Once we save a data into the session, we can obtain
it by specifying the storage key used to store the data. The first result above shows that when the value is not specified, all the data stored
is returned back. Also we can see that when the subkey argument is supplied, it only works when the data stored is an array that contains the the
specified subkey value. Hence, the value of the subkey is returned.
overwrite()
method is used to overwrite the entire value of a session. This means that the session value
is entirely replaced by the new value supplied. Usually, it is not advisable to overwrite the entire value of the global
session data. If there is a need to modify the entire session data, the best way to perform this is to fetch the entire session
value first. The value fetched can then be modified and then saved back using the overwrite()
method. In this way,
we are sure that no data will be lost.
Session::overwrite($newvalue)
where $newvalue: a new value that overides the previous session value.
$value = Session::value();// fetch current session value $value['some_key'] = "new_value"; // perform some modification Session::overwrite($value); // overwrite entire session value
Session::remove($key?, $subkey?);
where:
$key : key expected to be removed from session
$subkey : subkey that is expected to be removed from a key in database
$data1 = ['name'=>'foobaz']; $data2 = [ 'name1' => 'foo', 'name2' => 'bar', ]; Session::save('data1', $data1); Session::save('data2', $data2); Session::remove('data2', 'name2'); var_dump(Session::value()); //[ ['name'=>'foobaz'], ['name1' => 'foo']] var_dump(Session::value('data1')); // ['name1' => 'foobaz'] var_dump(Session::value('data2')); // ['name1' => 'foo'] Session::remove(); // remove entire session value var_dump(Session::value()); // empty
remove()
method will assume to remove the entire sesion data.