Heya all!

Even tho this tutorial has not ahd the attention i felt it would im still going to continue !


Today were going to be talking about sessions!

Sessions are a way of storing information about the current user on the site, there mainly used to store stuff like user data etc

now sessions are mainly used for logins etc and are only valid for short amount of time aftyer the user has stopped browsing the site

if you notice when you login to a website and go away for 30-40 mins and come back your logged out, well this is due to the session expiring !

theres ways to autostart sessions again using cookies etc but were going to do the basic needed stuff first!

theres 2 ways we can buid sessions
  1. Sessions stored on file
  2. Sessions stored in database


First were going to build a session mask.. this will act just like the Input handler but for sessions, then at a later date we can look at the different ways to assign handlers so you can use different methods to stare sessions!

any way lets look at a simple image how sessions work



Ok so now as you can see the session handler is what were building today but soon we will build another class that controls how the data is stored! for now were just fetching and setting!

ok so let get started with some code!

PHP Code: 
<?php
class Session
{
    
//This will hold the session varaibles
    
public $data = array();
    
    
//Construct
    
public function __construct()
    {
        
//make sure that session is not already active before we start it!
        
if(!session_id())
        {
            
//Set up some basic session info
            
ini_set('session.use_cookies''On');
            
ini_set('session.use_trans_sid''Off');
            
session_set_cookie_params(0'/');

            
//here we start session
            
session_start();
        }
        
//here we use the operator =& so whenever session OR data is edited then both are affected
        
$this->data =& $_SESSION;
    }

    
//Ok sets build a function to add data to the session
    
public function add($name,$value)
    {
        
$this->data[$name] = $value//can be object array string w.e
    
}
    
    
//This function will grab some data from session and if not exists then will return a var you specify else false
    
public function get($name,default = false)
    {
        return (isset(
$this->data[$name]) ? $this->data[$name] : false);
    }

    public function 
delete($name)
    {
        if(isset(
$this->data[$name]))
        {
            unset(
$this->data[$name]);
        }
    }

    
//ClearAll is good for logouts etc so it removes all active data from the session
    
public function clearAll()
    {
        
$this->data = array();
    }
}
?>
ok so now as you can see from above we have several things we can do with sessions now
  • Add
  • Get
  • Delete
  • ClearAll (Delete)


these are basics for now!

Ok save to Save the file in /system/Session.php

lets add it to the startup.php and assign it to the Registry so lets get that out the way

Open the startup.php and find
PHP Code: 
include SYSTEM_BASE_PATH '/engine/Input.php'
and after:
PHP Code: 
include SYSTEM_BASE_PATH '/engine/Session.php'
Ok lets add to the Registry system!

Find:
PHP Code: 
Registry::set('Input', new Input()); 
Add after:
PHP Code: 
Registry::set('Session', new Session()); 
Ok thats it for sessions now... im sure by now you can understand how you would use it!

When were finished with the framework we can build a script of your choice together for the community!
litewarez Reviewed by litewarez on . Tutorial [Creating a PHP Framework] {advanced} (PART 5) - Sessions Heya all! Even tho this tutorial has not ahd the attention i felt it would im still going to continue ! Today were going to be talking about sessions! Sessions are a way of storing information about the current user on the site, there mainly used to store stuff like user data etc now sessions are mainly used for logins etc and are only valid for short amount of time aftyer the user has stopped browsing the site Rating: 5