Multimedia Services    DVD Authoring    DVD Duplication    CD Replication    CD Packaging    Website Solutions    Silk Screening Printing Services
TXP Media - DVD Authoring - DVD / CD Replication / Multimedia Services
Home Company Services Quote Builder Info Contact Us

Storage Utilities

The session_manager class



In general, there are 2 types of data you need to take care of. The first is permanent data you wish to persist across multiple user visits. This is most easily handled by our database interface. However there is some data which only needs to be temporary to the user's current visit. For instance, if you are on an e-commerce site and a returning customer wishes to login. For security purposes you want to keep their username in the session to check throughout the site.

To accomidate this, we will need some way to manage the session. Ideally, we'd like to be able to just take a chunk of data and put it somewhere in the session. We would also like to organize the data in some sort of heirarchial fashion just to help with personal organization. Let's take some simple function calls which would accomplish this goal and then analyze how they can be built in PHP.

<?
    session_manager
::store('login_info','username',$username);
    
    
$stored_data session_manager::restore('login_info','username');
    
    echo 
'You are currently logged in as: '.$stored_data.'<BR>';
?>


A new introduction to this class is how we are statically calling the functions to interact with the session. Because the state of the session is always active, we have no reason to instantiate an instance of the session_manager class. All we need to do with the session requires no persistent state throughout the execution of the program. So let's start off by simply building our session class. First we will see how to store data to the session:

<?
    
class session_manager {
        function 
store() {
            
$_data array_pop($args);
            
            
$ref =& $_SESSION;
            for(
$i=0;$i<sizeof($args)-1;$i++) {
                if (!
is_array($ref[$args[$i]])) $ref[$args[$i]]=array();
                
$ref =& $ref[$args[$i]];
            }
            if (!
is_array($ref[$args[$i]])) $ref[$args[$i]]=array();
            
$ref[$args[$i]]=$_data;
        }
    }
?>


This function seems a bit complicated at first but we are really trying to accomplish something simple here. The last piece of the function arguments we send will always be the piece of data we wish to store. This can be any sort of elementary data or an array of elementary data (like an array of strings and numbers). So we'll take that out of the arguments list and put it into a special temporary variable. Now we need to traverse the $_SESSION variable to get to the point where we want to place this variable. PHP since 4.3 keeps all session data into the superglobal $_SESSION. By traveling through the $_SESSION array level by level through reference, we can eventually dig down in the tree to the location where we wish to place our useful data. At that point we place the data in the session and we are done with our execution.

The advantage to storing session data this way is you can logically arrange your session information to avoid confusion. Everything related to login data you can store in the session under a 'login_info' container like this: session_manager::store('login_info','param1',$some_variable1), or session_manager::store('login_info','param2',$some_variable2). Now that we can write to the session, our next goal is to read from the session:

<?
    
function restore() {
        
$args func_get_args();
        
$ref =& $_SESSION;
        for(
$i=0;$i<sizeof($args);$i++) {
            
$ref =& $ref[$args[$i]];
        }
        return 
$ref;
    }
?>


Restoring is much simpler than writing to the session. When you restore from the session, we can take advantage once again of our simple way of organizing our session data. By passing a set of arguments to the function we can tell the session manager where to find the piece of data we desire. If we place the current user's username in the login_info container we can retrieve it like this: $current_user = session_manager::restore('login_info','username');.

At this point we have basic ways to communicate with the database and the session. Generally these are the only ways you will need to store data. The database becomes the storage container for permanent data while the session is the storage container for temporary data for that user's current visit to your site.

With our basic storage in place, we can start building our most basic class. When we build this class we will take into consideration how to generalize all functions. This includes when you need to write this class to the session or the database and seamlessly restore it from either container. In the next section we will discuss how to do this.

Tutorial Under Construction - More Content is Added Weekly
Login:
Pass:
TXP Media  |  Company  |  Multimedia Services  |  Contact Us
Sitemap  |  Resources

Multimedia Tutorials and Technology Related Information Multimedia Tutorials and Technology Related Information



Copyright 2003-2017 TXP Media. All rights reserved.