PHP Class - Session Handling

Discussion in 'PHP' started by NatalicWolf, May 22, 2009.

  1. #1
    Here is a very basic class for Session handling.

    
    // Example Usage:
    include('classes/session.php');
    
    $Session = new Session('Script');
    
    // Clean setting of the session data
    $Session->Set('user','username');
    $Session->Set('pass','password');
    
    // Clean retrieving of the data
    $User = $Session->Get('user');
    
    // Clean retrieving of the data, with a default value.
    
    $User = $Session->Get('user','username'); // If no value, returns 'username'
    
    
    PHP:
    session.php
    
    <?PHP
        /*
            Code developed by Thomas Andrews (thomas.p.andrews@gmail.com)
           
            Code is free to use.
        */
    session_start();
    class Session
    {
        var $SessionName='Default';
        
        /* Function name: Constructor
        Params:
            @SessionName - The session name
        */
        function __constructor($SessionName)
        {
            $this->SessionName=$SessionName;
        }
        /* Function name: Set
        Params:
            @Setting - The key to set
            @Value - The value to set
        */
        function Set($Setting,$Value)
        {
            $_SESSION[$this->SessionName][$Setting]=$Value;
        }
        /* Function name: Get
        Params:
            @Setting - The key to get
            @Default - Value to return if the requested key is empty.
        */
        function Get($Setting,$Default='')
        {
            if(isset($_SESSION[$this->SessionName][$Setting]) && !empty($_SESSION[$this->SessionName][$Setting]))
                return $_SESSION[$this->SessionName][$Setting];
            else
                return $Default;
        }
       
    }
    ?>
    
    PHP:
    Any questions, please feel free to ask.
     
    NatalicWolf, May 22, 2009 IP
  2. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Thanks a lot for this script. Appreciated.
     
    alfa_375, May 23, 2009 IP