GitXplorerGitXplorer
r

Session-Class

public
21 stars
11 forks
3 issues

Commits

List of commits on branch master.
Unverified
78b7d3f82e2a51d40023001df1dc86a77f942ebd

Adding check for sessoin key and moving new time

committed 10 years ago
Unverified
03716309602cc84683adb20b7e48ab4b4098af08

Update for psr-0 and add composer

rrcastera committed 11 years ago
Unverified
359cb8e0013caa402593e5f5f853a58c4214cd26

Partial rewrite, add examples and markdown

rrcastera committed 13 years ago
Unverified
bbb55c0a27db30bbf796d2b3f431016337f724a5

modify comments

rrcastera committed 14 years ago
Unverified
8b0dd2216d7dd50f54b83cd6e25eb2a732bccab9

Added readme

rrcastera committed 14 years ago
Unverified
aa675ca16902ab9f23d1276180b369f807543c19

change to comments

rrcastera committed 14 years ago

README

The README file for this repository.

Session Class

This is a Session class for you guessed it, managing sessions. Back in the day, when there weren't many open source frameworks on the market, developers had to code things from scratch to achieve certain functionality. This class's inception, is the from one of those instances. With this class you can easily time-out your authenticated users after a specified interval.

Setup


Add a composer.json file to your project:

{
  "require": {
      "rcastera/session": "v1.0.0"
  }
}

Then provided you have composer installed, you can run the following command:

$ composer.phar install

That will fetch the library and its dependencies inside your vendor folder. Then you can add the following to your .php files in order to use the library (if you don't already have one).

require 'vendor/autoload.php';

Then you need to use the relevant class, and instantiate the class. For example:

Getting Started


require 'vendor/autoload.php';

use rcastera\Browser\Session\Session;

$session = new Session();

Examples


Logging in. (login.php)
<?php
    require 'vendor/autoload.php';
    use rcastera\Browser\Session\Session;

    $errors = array();

    // You'll definitely want to add more validation here and check against a
    // database or something. This is just an example.
    if (! empty($_POST)) {
        if ($_POST['username'] == 'test' && $_POST['password'] == 'test') {
            $session = new Session();

            // You can define what you like to be stored.
            $user = array(
                'user_id' => 1,
                'username' => $_POST['username']
            );

            $session->register(120); // Register for 2 hours.
            $session->set('current_user', $user);
            header('location: index.php');
            exit;
        } else {
            $errors[] = 'Invalid login.';
        }
    }
?>

// Your form here.
Secure area once authenticated. (index.php/controller/whatever)
<?php
    require 'vendor/autoload.php';
    use rcastera\Browser\Session\Session;

    $session = new Session();

    // Check if the session registered.
    if ($session->isRegistered()) {
        // Check to see if the session has expired.
        // If it has, end the session and redirect to login.
        if ($session->isExpired()) {
            $session->end();
            header('location: login.php');
            exit;
        } else {
            // Keep renewing the session as long as they keep taking action.
            $session->renew();
        }
    } else {
        header('location: login.php');
        exit;
    }
?>
Logging out. (logout.php)
<?php
    require 'vendor/autoload.php';
    use rcastera\Browser\Session\Session;

    $session = new Session();
    $session->end();
    header('location: login.php');
    exit;
?>

Contributing


  1. Fork it.
  2. Create a branch (git checkout -b my_branch)
  3. Commit your changes (git commit -am "Added something")
  4. Push to the branch (git push origin my_branch)
  5. Create an Issue with a link to your branch
  6. Enjoy a refreshing Coke and wait