PHP Sessions wrapper

They have: 218 posts

Joined: Apr 2001

I want to wrap the following PHP session variables (from Mark Hensler) in a function. Any custom user functions to demonstrate this?

session_start();

$sess_expire = '1';

$time_stamp = mktime() + ( $sess_expire * 60 );

$enc_time = md5( $time_stamp );

$sid = session_id();

$SID = SID;

// echo $sid;

// # set session vars

$_SESSION['sess_timestamp'] = $time_stamp;

$_SESSION['sess_key'] = $enc_time . $sid;

$_KEY = $_SESSION['sess_key'];

Thanks,

TM

They have: 218 posts

Joined: Apr 2001

Actually, here's a starting point..seems to work.

<?php
class Session {
  
function
Session(){
    }

function
setVars(){
   global
$_KEY, $_SESSION,$SID,$sid;

  
session_start();
  
$sess_expire = '1';
$time_stamp = mktime() + ( $sess_expire * 60 );

$enc_time = md5( $time_stamp );
$sid = session_id();
$SID = SID;

$_SESSION['sess_timestamp'] = $time_stamp;
$_SESSION['sess_key'] = $enc_time . $sid;
$_KEY = $_SESSION['sess_key'];
       }
}


// $s = new Session();
// $s->setVars();
?>

TM

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

<?php
class Session
{

   
// public members
   
var $key = NULL;
    var
$sid = NULL;
    var
$sess_expire = 1;

   
// private members
   
var $_sess_timestamp = 1;

    function
Session()
    {
       
session_start();
       
$this->sid = session_id();
    }

    function
setVars()
    {
       
$this->_sess_timestamp = mktime() + ($this->sess_expire * 60);
       
$this->key = md5($this->_sess_timestamp) . $this->sid;

       
$_SESSION['sess_timestamp'] = $this->_sess_timestamp;
       
$_SESSION['sess_key'] = $this->key;
    }
}

$s = new Session();
//$s->sess_expire = 2; // if ya wanna change the expire time
$s->setVars();

//$s->sid = session id  (instead of $sid)
//$s->key = session key (instead of $_KEY)
?>

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.