Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
217 lines (193 sloc) 5.57 KB

Session

The Session provides safe way to work with sessions.

Installation

composer require webiik/session

Example

$session = new \Webiik\Session\Session();
$session->setToSession('foo', 'bar');
if ($session->isInSession('foo')) {
    echo 'Session foo has value: ' . $session->getFromSession('foo');
}
$session->delFromSession('foo');

NOTICE: If you can, don't forget to call session_write_close() before time intensive operations. Otherwise users of your app can experience serious lags.

Configuration

setDomain

setDomain(string $domain): void

setDomain() sets the (sub)domain that the session cookie is available to.

$session->setDomain('mydomain.tld');

setUri

setUri(string $uri): void

setUri() sets the path on the server in which the session cookie will be available on.

$session->setUri('/');

setSecure

setSecure(bool $bool): void

setSecure() indicates that the session cookie should only be transmitted over a secure HTTPS connection from the client. The default value is FALSE.

$session->setSecure(true);

setHttpOnly

setHttpOnly(bool $bool): void

setHttpOnly() indicates that the session cookie should only be accessible through the HTTP protocol. The default value is FALSE.

$session->setHttpOnly(true);

setSessionName

setSessionName(string $name): void

setSessionName() sets the name of the session which is used as cookie name. The default value is PHPSESSID.

$session->setSessionName('mySessionName');

setSessionDir

setSessionDir(string $path): void

setSessionDir() defines the argument which is passed to the save handler. If you choose the default files handler, this is the path where the files are created.

$session->setSessionDir(__DIR__ . '/tmp');

setSessionGcProbability

setSessionGcProbability(int $sessionGcProbability): void

setSessionGcProbability() in conjunction with setSessionGcDivisor() is used to manage probability that the gc (garbage collection) routine is started. The default value is 1.

$session->setSessionGcProbability(1);

setSessionGcDivisor

setSessionGcDivisor(int $sessionGcDivisor): void

setSessionGcDivisor() coupled with setSessionGcProbability() defines the probability that the gc (garbage collection) process is started on every session initialization. The probability is calculated by using GsProbability/GcDivisor, e.g. 1/100 means there is a 1% chance that the GC process starts on each request. The default value of GcDivisor is 100.

$session->setSessionGcDivisor(100);

setSessionGcLifetime

setSessionGcLifetime(int $sec): void

setSessionGcLifetime() specifies the number of seconds after which data will be seen as 'garbage' and potentially cleaned up. Garbage collection may occur during session start (depending on setSessionGcProbability() and setSessionGcDivisor()).

$session->setSessionGcLifetime(1440);

Note: If different scripts have different values of gcLifetime but share the same place for storing the session data then the script with the minimum value will be cleaning the data. In this case, use this directive together with setSessionDir().

Adding

setToSession

setToSession(string $key, $value): void

setToSession() sets $value to the session under given $key.

$session->setToSession('foo', 'bar');

addToSession

addToSession(string $key, $value): void

addToSession() ads $value to the session under given $key.

$session->addToSession('foo', 'bar');

sessionStart

sessionStart(): bool

sessionStart() starts the session. Use it when you need to work directly with $_SESSION global.

$session->sessionStart();

sessionRegenerateId

sessionRegenerateId(): void

sessionRegenerateId() replaces the current session id with a new one, and keeps the current session information. Also the original session is kept.

$session->sessionRegenerateId();

Check

isInSession

isInSession(string $key): bool

isInSession() determines if $key is set in session and if its value is not NULL.

$session->isInSession('foo');

Getting

getFromSession

getFromSession(string $key)

getFromSession() gets value from the session by $key.

$session->getFromSession('foo');

getAllFromSession

getAllFromSession()

getAllFromSession() returns all values stored in the session.

$session->getAllFromSession();

Deletion

delFromSession

delFromSession(string $key): void

delFromSession() removes value from the session by $key.

$session->delFromSession('foo');

dellAllFromSession

dellAllFromSession(): void

dellAllFromSession() removes all values from the session.

$session->dellAllFromSession();

sessionDestroy

sessionDestroy(): bool

sessionDestroy() removes all values from the session and un-sets the session.

$session->sessionDestroy();

Resources

You can’t perform that action at this time.