1

i have a 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 a single session value i use

  Session::set(Name, value);

To set a multiple session value i use

  Session::set(Name, array(name, value....));

Then to simply retrieve the session i require i use:

  Session::get(Name);

Now my problem is that when i can set multi dimension values, i cannot actually get them with the current get function above. SO how can i re-structure my get($key) so that it not only returns a single request but also any request

  Session::set('joe', 'bloggs');
  Session::set('beth', array('age'=>'21','height'=>'5.5ft'));

So for instance again I get a single session value EG Joe:

    Session::get('joe');

Then if i want to get beth's details i can assign the session value to a var and then access the inner details like so:

   $age = Session::get('beth');
   $age['age']

But would it be possible to reconstruct my get method so i can do:

  Session::get('beth','age');

Or would it be best to stick to what i am currently doing.

Thanks

0

5 Answers 5

3

You can update your get method to 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 you can use it like:

Session::get('joe'); // returns array
Session::get('joe', 'age'); // returns joe's age
Sign up to request clarification or add additional context in comments.

Comments

1

Here is what you need

Session::set('joe', 'bloggs');
Session::set('beth', array('age' => '21','height' => '5.5ft','ball' => array("A" => "blue","B" => "Green")));

var_dump(Session::get("beth", "age")); // returns 21
var_dump(Session::get("beth", "ball", "A")); // returns blue

Your class modified

class Session {

    private static function init() {
        if (session_status() != PHP_SESSION_ACTIVE)
            session_start();
    }

    public static function set($key, $value) {
        self::init();
        $_SESSION[$key] = $value;
    }

    public static function get() {
        self::init();
        $args = func_get_args(); // Get all arguments
        $items = $_SESSION; // create a tempoary version of $_SESSION aarray
        foreach ( $args as $ndx ) {
            // Take Item and override with Subitem
            $items = isset($items[$ndx]) ? $items[$ndx] : null;
        }
        return $items;
    }
}

6 Comments

Nice way of using the $_SESSION array and override it with the first found match and then matching it again with the next arg. But you'll should add some comments to make the code clear and readable for the non-PHP-experts.
Never knew that was not clear ... updated with simple comments
So @Sven when you mention this being a nice way would this be a better method than the one demo'd ? It's a little bigger for the same effect but are they identical or better
@SimonDavies The are both good answers. But you need to look at what you need. If you are going/wanting to use it as Session::get(arg, arg2, ..) then use this one, otherwise keep it simple. ;)
@SimonDavies Sven answer is only valid if you are using two argument .. but if you would end up using more than 2 argument then use this ...
|
0

Be careful. That may be confusing to some of us, as in some frameworks, the second parameter in a getter means a default value:

Session::get('key', 'defaultValue');

defaultValue would be returned if key is not present.

Probably not the best solution, but you could convert everything you set to a special class (maybe called KeyValue or whatever) and, when you use get, you could return either the value of the key OR another KeyValue object, with an implementation of get(), doing the same once again, returning the value OR another KeyValue. That way, you would be able to method chain in a rather cool way:

Session::get('beth')->get('age')

Comments

0

Function like this could be used with unlimited number of keys:

public static function get()
{
    $array = $_SESSION;
    $keys = func_get_args();
    foreach($keys as $key)
    {
        if(isset($array[$key]))
            $array = $array[$key];
        else
            return FALSE; // or it could return array()
    }

    return $array;
}

Comments

0

In Core PHP or other any framework of PHP for showing all the data in SESSION

   print_r($_SESSION);
   public function setSessionValue($session_name, $session_value){
         $_SESSION[$session_name] = $session_value;
   }

   public function getSessionValue($session_name){
         return $_SESSION[$session_name];
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.