communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "sessions and co" - download "sessions class"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

sessions and co - sessions class was created/last modified on Fri 02 Apr 2004 8:41 AM

<?php

class Session
{
    
    
// Start a new session
    // Syntax: $session = new session();
    
    
function Session()
    {
        
session_start();
    }
            
    
// Set a variable in a current session
    // Syntax: $session->set_var("Var name", "Value");
    
    
function set_var($varname,$varvalue)
    {
        if(!isset(
$varname) || !isset($varvalue) )
        {
            die(
'Function setvar( String $varname, mixed $value ) expects two parameters!');
        }
        if(
phpversion() >= "4.1.0")
        {
            
$_SESSION[$varname] = $varvalue;
        }
        if(!isset(
$GLOBALS[$varname]) )
        {
            
$GLOBALS[$varname] = $varvalue;
        }else{
            global
$HTTP_SESSION_VARS;
            
session_register($varname);
            
$GLOBALS['HTTP_SESSION_VARS'][$varname] = $varvalue;
            if(!isset(
$GLOBALS[$varname]) )
            {
                
$GLOBALS[$varname] = $varvalue;
    
            }
        }
    }
            
    
// Get a variable from current session
    // Syntax: $variable = $session->get_var("var name");
    // echo "$variable" to get grabbed session variable
    
    
function get_var( $varname )
    {
        if(!isset(
$varname))
        {
            die(
'Function getvar( String $varname ) expects a parameter!');
        }
        if(
phpversion() >= "4.1.0" )
        {
            if (isset(
$GLOBALS[$varname]))
            {
                return
$GLOBALS[$varname];
            }
            elseif (isset(
$GLOBALS['_SESSION'][$varname]))
            {
                
$GLOBALS[$varname] = $GLOBALS['_SESSION'][$varname];
                return
$GLOBALS['_SESSION'][$varname];
            }
        }else{
            if (isset(
$GLOBALS[$varname]))
            {
                return
$GLOBALS[$varname];
            }
            elseif (isset(
$GLOBALS['HTTP_SESSION_VARS'][$varname]))
            {
                
$GLOBALS[$varname] = $GLOBALS['HTTP_SESSION_VARS'][$varname];
                return
$GLOBALS['HTTP_SESSION_VARS'][$varname];
            }
        }
    }
            
    
// Get a current session string (EXAMPLE: PHPSESSID=b188e8c9c45b347cdded2)
    // Syntax: $sid = $session->get_sid_string();
    // echo "$sid" to grab the string id.  By default it uses PHPSESSID= variable
    
    
function get_sid_string()
    {
        return
session_name() . "=" . session_id();
    }
    
    
// Get current session ID (EXAMPLE: whatever=b188e8c9c45b347cdded2)
    // Syntax: $sid = $session->get_sid();
    
    
function get_sid()
    {
        return
session_id();
    }
    
    
// Unset a current session variable
    // Syntax: $session->var_unset("variable name");
    
    
function var_unset($varname)
    {
        if(!isset(
$varname))
        {
            die(
'Function var_unset( String $varname ) expects a parameter!');
        }
        if(
phpversion() >= '4.1.0')
        {
            if(isset(
$GLOBALS[$varname]))
            {
                unset(
$GLOBALS[$varname] );
            }
            if (isset(
$GLOBALS['_SESSION'][$varname]))
            {
                unset(
$GLOBALS['_SESSION'][$varname]);
            }
        }else{
            if (isset(
$GLOBALS[$varname]))
            {
                unset(
$GLOBALS[$varname] );
            }
            if (isset(
$GLOBALS['HTTP_SESSION_VARS'][$varname]))
            {
                unset(
$GLOBALS['HTTP_SESSION_VARS'][$varname]);
            }
        }
    }
    
    
// Unset every variable in the current session
    // Syntax: $session->ses_unset();
    
    
function ses_unset()
    {
        if(
phpversion() >= '4.1.0')
        {
            if(isset(
$GLOBALS['_SESSION'])) $a = $GLOBALS['_SESSION'];
            {
                while(list(
$key,) = each($a))
                {
                    
$this->var_unset($key);
                }
            }
        }else{
            if(isset(
$GLOBALS['HTTP_SESSION_VARS']))
            {
                
$a = $GLOBALS['HTTP_SESSION_VARS'];
                while(list(
$key,) = each($a))
                {
                    
$this->var_unset($key);
                }
            }
        }
    }
    
    
// This will delete your current session and delete every session variable created
    // Syntax: $session->destroy();
    
    
function destroy()
    {
        
$this->ses_unset();
        
session_destroy();
    }
            
    
// Display all variables in a current session
    // Syntax: $session->show();
    
    
function show()
    {
        echo
'Variables set in current session:<br>';
        if(
phpversion() >= '4.1.0')
        {
            if(isset(
$GLOBALS['_SESSION']))
            {
                
$a = $GLOBALS['_SESSION'];
                while(list(
$key,$value) = each($a))
                {
                    echo
'<b>Variable:</b> '.$key.' - <b>Value:</b> '.$value.'<br>';
                }
            }
        } else {
            if(isset(
$GLOBALS['HTTP_SESSION_VARS']))
            {
                
$a = $GLOBALS['HTTP_SESSION_VARS'];
                while(list(
$key,$value) = each($a))
                {
                    echo
'<b>Variable:</b> '.$key.' - <b>Value:</b> '.$value.'<br>';
                }
            }
        }
    }
}

?>

learn more about some of the PHP functions used in this snippet: current, delete, die, each, echo, else, elseif, function, glob, global, isset, key, list, phpversion, return, session_destroy, session_id, session_name, session_register, session_start, unset, var, while

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

go to PHParadise to get free PHP, Javascript and FlashMX code