improve session ID assignment to avoid possible overlaps.

Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
This commit is contained in:
Taylor Otwell
2012-02-28 10:06:53 -06:00
parent e4c03aab37
commit b37c966aea
8 changed files with 53 additions and 22 deletions

View File

@@ -1,6 +1,6 @@
<?php namespace Laravel\Session\Drivers;
class APC implements Driver {
class APC extends Driver {
/**
* The APC cache driver instance.

View File

@@ -2,7 +2,7 @@
use Laravel\Crypter;
class Cookie implements Driver {
class Cookie extends Driver {
/**
* The name of the cookie used to store the session payload.

View File

@@ -3,7 +3,7 @@
use Laravel\Config;
use Laravel\Database\Connection;
class Database implements Driver, Sweeper {
class Database extends Driver implements Sweeper {
/**
* The database connection.

View File

@@ -1,6 +1,6 @@
<?php namespace Laravel\Session\Drivers;
<?php namespace Laravel\Session\Drivers; use Laravel\Config, Laravel\Str;
interface Driver {
abstract class Driver {
/**
* Load a session from storage by a given ID.
@@ -10,7 +10,7 @@ interface Driver {
* @param string $id
* @return array
*/
public function load($id);
abstract public function load($id);
/**
* Save a given session to storage.
@@ -20,7 +20,7 @@ interface Driver {
* @param bool $exists
* @return void
*/
public function save($session, $config, $exists);
abstract public function save($session, $config, $exists);
/**
* Delete a session from storage by a given ID.
@@ -28,6 +28,43 @@ interface Driver {
* @param string $id
* @return void
*/
public function delete($id);
abstract public function delete($id);
/**
* Insert a fresh session and return the payload array.
*
* @return array
*/
public function fresh()
{
// We will simply generate an empty session payload array, using an ID
// that is not currently assigned to any existing session within the
// application and return it to the driver.
return array('id' => $this->id(), 'data' => array(
':new:' => array(),
':old:' => array(),
));
}
/**
* Get a new session ID that isn't assigned to any current session.
*
* @return string
*/
public function id()
{
$session = array();
// We'll containue generating random IDs until we find an ID that is
// not currently assigned to a session. This is almost definitely
// going to happen on the first iteration.
do {
$session = $this->load($id = Str::random(40));
} while ( ! is_null($session));
return $id;
}
}

View File

@@ -1,6 +1,6 @@
<?php namespace Laravel\Session\Drivers;
class File implements Driver, Sweeper {
class File extends Driver implements Sweeper {
/**
* The path to which the session files should be written.

View File

@@ -1,6 +1,6 @@
<?php namespace Laravel\Session\Drivers;
class Memcached implements Driver {
class Memcached extends Driver {
/**
* The Memcache cache driver instance.

View File

@@ -1,6 +1,6 @@
<?php namespace Laravel\Session\Drivers;
class Redis implements Driver {
class Redis extends Driver {
/**
* The Redis cache driver instance.