+ Reply to Thread
Results 1 to 2 of 2




  

Thread: Session arrays in class

      
  1. #1
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default Session arrays in class

    Hey guys, here's a little problem for you, I need to be able to set and get session values from with a class by calling a function, not a problem however some of the session variables will be arrays

    e.g.
    PHP Code:
    $_SESSION['basesession']['is_admin'] = 1;
    $_SESSION['basesession']['page_access'][] = 'index';
    $_SESSION['basesession']['page_access'][] = 'mydetails';
    $_SESSION['basesession']['admin_details']['title'] = 'Mr'
    As you can see page_access is an array which I would need to be able to do for loops with and check for in_array as well but I cant figure out how to do it.

    PHP Code:
    function get($VALUE){
      if (!
    is_array($VALUE)){
        return 
    $_SESSION['basesession'][$VALUE];
      } else {
        foreach (
    $VALUE as $key=>$val){
          
    $test .= '['.$key.']';
        }
        return (
    $_SESSION['basesession'].$$test);
      }

    That's the get function I made to get the values which doesn't work, a print_r($_SESSION) prints the following code
    Session Dump
    Code:
    Array
    (
        [basesession] => Array
            (
                [is_logged_in] => 1
                [page_access] => Array
                    (
                        [0] => index
                        [1] => mydetails
                    )
                [admin_details] => Array
                    (
                        [title] => Mr
                    )
            )
    )

  2. #2
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default Re: Session arrays in class

    Well I managed to solve this one myself, for anyone who wants the functions they are listed below.

    PHP Code:
        function get($KEY){
            if (
    strpos($KEY':')){
                
    // it will be an array
                
    $fields explode(':'$KEY);
                
    $eval '$_SESSION[\'user_login_manager\']';
                for (
    $i 0$i count($fields); $i++){
                    
    $eval .= '[\''.$fields[$i].'\']';
                }
                eval(
    "\$thevar = ".$eval.";");
                return 
    $thevar;
            } else {
                return 
    $_SESSION['user_login_manager'][$KEY];
            }
        }

        function 
    set($KEY$VALUE){
            if (
    strpos($KEY':')){
                
    $eval "\$_SESSION['user_login_manager']";
                
    $fields explode(":"$KEY);
                for (
    $i 0$i count($fields); $i++){
                    if (
    strlen($fields[$i]) > 0)$eval .= "['".$fields[$i]."']";
                    else 
    $eval .= "[".$fields[$i]."]";
                }
                if (
    is_string($VALUE))$eval .= " = '".$VALUE."';";
                else 
    $eval .= " = ".$VALUE.";";
                
                echo 
    "<i>".$eval."</i>\n";
                eval(
    $eval);
            } else {
                
    $_SESSION['user_login_manager'][$KEY] = $VALUE;
            }
        }

    //usage
    // to set $_SESSION['user_login_manager']['test']
    set('test''value');
    set('var1:var2''value');//['user_login_manager']['var1']['var2']
    set('var1:'value);//['user_login_manager']['var1'][] 
    Matthew Bagley
    Paramiliar Design Studios
    IT Consultant | Website Design | Website Development

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. [cPanel-News] Free cPanel Conference Session Videos To Be Released
    By vectro in forum Dedicated / VPS Hosting
    Replies: 0
    Last Post: Jan 30th, 2010, 10:18 pm
  2. Replies: 13
    Last Post: Mar 23rd, 2009, 4:24 am
  3. Replies: 0
    Last Post: Dec 18th, 2008, 4:33 am
  4. Replies: 1
    Last Post: Nov 8th, 2008, 2:00 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
WebTalkForums
WebTalkForums
Recent Forum Threads