PHP Session Class return values -
i have simplet session class (below):
class session{ public static function init(){ @session_start(); } public static function set($key, $value){ $_session[$key] = $value; } public static function get($key){ if (isset($_session[$key])) { return $_session[$key]; } else if(empty($key)){ return $_session; } } public static function destroy(){ session_destroy(); } } to set single session value use
session::set(name, value); to set multiple session value use
session::set(name, array(name, value....)); then retrieve session require use:
session::get(name); now problem when can set multi dimension values, cannot them current function above. how can re-structure get($key) not returns single request request
session::set('joe', 'bloggs'); session::set('beth', array('age'=>'21','height'=>'5.5ft')); so instance again single session value eg joe:
session::get('joe'); then if want beth's details can assign session value var , access inner details so:
$age = session::get('beth'); $age['age'] but possible reconstruct method can do:
session::get('beth','age'); or best stick doing.
thanks
you can update get method this:
public static function get($key, $item = null){ if (isset($_session[$key])) { if(isset($item) && isset($_session[$key][$item])) { return $_session[$key][$item]; } return $_session[$key]; } return null; //not found } then can use like:
session::get('joe'); // returns array session::get('joe', 'age'); // returns joe's age
Comments
Post a Comment