Session
in package
implements
SessionHandlerInterface
Uses
BackwardCompatibility
Implementation of PHP's session API.
What it does:
- it handles the session data in the database (more scalable.)
- it uses the databaseSession_lifetime setting for garbage collection.
- the custom session handler is set by Session::load().
Table of Contents
Interfaces
- SessionHandlerInterface
Properties
- $backcompat : array<string|int, mixed>
Methods
- close() : bool
- Closes the session.
- destroy() : bool
- Destroys a session.
- exportStatic() : void
- Provides a way to export a class's public static properties and methods to global namespace.
- gc() : int|false
- Cleans up old sessions.
- load() : void
- Attempt to start the session, unless it already has been.
- open() : bool
- Initializes the session.
- read() : string
- Read session data.
- sessionClose() : bool
- Backward compatibility wrapper for the close method.
- sessionDestroy() : bool
- Backward compatibility wrapper for the destroy method.
- sessionGC() : int|false
- Backward compatibility wrapper for the gc method.
- sessionOpen() : bool
- Backward compatibility wrapper for the open method.
- sessionRead() : string
- Backward compatibility wrapper for the read method.
- sessionWrite() : bool
- Backward compatibility wrapper for the write method.
- write() : bool
- Writes session data.
Properties
$backcompat
private
static array<string|int, mixed>
$backcompat
= ['func_names' => ['load' => 'loadSession']]
BackwardCompatibility settings for this class.
Methods
close()
Closes the session.
public
close() : bool
Return values
bool —Always returns true.
destroy()
Destroys a session.
public
destroy(string $session_id) : bool
Parameters
- $session_id : string
-
The session ID.
Return values
bool —Whether the session was successfully destroyed.
exportStatic()
Provides a way to export a class's public static properties and methods to global namespace.
public
static exportStatic() : void
To do so:
- Use this trait in the class.
- At the END of the class's file, call its exportStatic() method.
Although it might not seem that way at first glance, this approach conforms to section 2.3 of PSR 1, since executing this method is simply a dynamic means of declaring functions when the file is included; it has no other side effects.
Regarding the $backcompat items:
A class's static properties are not exported to global variables unless explicitly included in $backcompat['prop_names']. Likewise, a class's static methods are not exported as global functions unless explicitly included in $backcompat['func_names'].
$backcompat['prop_names'] is a simple array where the keys are the names of one or more of a class's static properties, and the values are the names of global variables. In each case, the global variable will be set to a reference to the static property. Static properties that are not named in this array will not be exported.
$backcompat['func_names'] is a simple array where the keys are the names of one or more of a class's static methods, and the values are the names that should be used for global functions that will encapsulate those methods. Methods that are not named in this array will not be exported.
Adding non-static properties or methods to the $backcompat arrays will produce runtime errors. It is the responsibility of the developer to make sure not to do this.
gc()
Cleans up old sessions.
public
gc(int $max_lifetime) : int|false
Parameters
- $max_lifetime : int
-
Sessions that have not updated for the last $max_lifetime seconds will be removed.
Return values
int|false —The number of deleted sessions, or false on failure.
load()
Attempt to start the session, unless it already has been.
public
static load() : void
open()
Initializes the session.
public
open(string $path, string $name) : bool
Parameters
- $path : string
-
The path to save the session to.
- $name : string
-
The name of the session.
Return values
bool —Always returns true.
read()
Read session data.
public
read(string $session_id) : string
Note: The PHP manual says to return false if no record was found, but doing so causes errors on some versions of PHP when the user logs out. Returning an empty string works for all versions.
Parameters
- $session_id : string
-
The session ID.
Return values
string —The session data.
sessionClose()
Backward compatibility wrapper for the close method.
public
static sessionClose() : bool
Return values
boolsessionDestroy()
Backward compatibility wrapper for the destroy method.
public
static sessionDestroy(string $session_id) : bool
Parameters
- $session_id : string
Return values
boolsessionGC()
Backward compatibility wrapper for the gc method.
public
static sessionGC(int $max_lifetime) : int|false
Parameters
- $max_lifetime : int
Return values
int|falsesessionOpen()
Backward compatibility wrapper for the open method.
public
static sessionOpen(string $path, string $name) : bool
Parameters
- $path : string
- $name : string
Return values
boolsessionRead()
Backward compatibility wrapper for the read method.
public
static sessionRead(string $session_id) : string
Parameters
- $session_id : string
Return values
stringsessionWrite()
Backward compatibility wrapper for the write method.
public
static sessionWrite(string $session_id, string $data) : bool
Parameters
- $session_id : string
- $data : string
Return values
boolwrite()
Writes session data.
public
write(string $session_id, string $data) : bool
Parameters
- $session_id : string
-
The session ID.
- $data : string
-
The data to write to the session.
Return values
bool —Whether the info was successfully written.