Bloody mess of a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brandonhowlett
Forum Newbie
Posts: 2
Joined: Sun Mar 08, 2009 2:30 pm

Bloody mess of a function

Post by brandonhowlett »

I just can't wrap my head around this one. I'm using array cookies (ie: $_COOKIE['site']['dir']['SID']) and I couldn't find anything to handle them so I wrote this function:

Code: Select all

 
// Cookie set on prior page... notice cookie naming convention
setcookie('site[dir][SID]', $session_id...);
 
function stackCookies()
{
    if ( isset($_SERVER['HTTP_COOKIE']) )
    {
        //print $_SERVER['HTTP_COOKIE'];// results below
        //site[dir][SID]=855rq5m9Re1I7bJ1L5b2d5dD7Z4rs8A8; site[dir][USR]=username; site=d0ba6a76ddb885eda8478da46c66ee1eArray
 
        // Split $_SERVER['HTTP_COOKIE'] into individual cookies
        $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
        
        foreach ( $cookies as $cookie )
        {
            // Split to $keys = $value pairs
            $parts = explode('=', trim($cookie));
            
            // Split string into array -> change to preg_match_all()
            $tmp_keys = str_replace(array('][','[',']'), array('_','_',''), $parts[0]);
            $keys = explode('_', $tmp_keys);
            $value = $parts[1];
            
            // Stack 2d array $keys as 3d array -> change to loop
            if ( count($keys) == 4 )
            {
                $cookie_array[$keys[0]][$keys[1]][$keys[2]][$keys[3]] = $value;
            }
            elseif ( count($keys) == 3 )
            {
                $cookie_array[$keys[0]][$keys[1]][$keys[2]] = $value;
            }
            elseif ( count($keys) == 2 )
            {
                $cookie_array[$keys[0]][$keys[1]] = $value;
            }
        }
        
        return $cookie_array;
    }
    
    return NULL;
}
 
print_r(stackCookies());
 
Result:

Code: Select all

 
Array
(
    [site] => Array
        (
            [dir] => Array
                (
                    [SID] => 855rq5m9Re1I7bJ1L5b2d5dD7Z4rs8A8
                    [USR] => username
                )
 
        )
 
)
 
The function works, but I know that it can be simplified, A LOT. I'd like to use preg_match_all to extract each array level ie: $parts[0]="site[dir][SID]" -> $array[0]="site", $array[1]="dir", $array[2]="SID". I was trying something like:

Code: Select all

 
preg_match_all("/\[[\w+]\]/", $parts[0], $matches, PREG_SET_ORDER);
 
But alas, no dice.

For the life of me I can't figure out how to loop through the flat array of $keys and convert it to a stacked array like the results above. Any help is appreciated. :banghead:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Bloody mess of a function

Post by Benjamin »

Why not just use serialize() and unserialize() to store the array of data in the cookie?
brandonhowlett
Forum Newbie
Posts: 2
Joined: Sun Mar 08, 2009 2:30 pm

Re: Bloody mess of a function

Post by brandonhowlett »

I considered that but my cookies have varying expirations. This function allows me to retrieve $COOKIE['site']['dir']['SID'], just as I would $_COOKIE['SID'].
Post Reply