This Session
class allows you to manage sessions in your application.
This static method is responsible for initializing the session.
By default this method initializes the session by setting the following options:
- "name": "mk4u"
- "use_cookies": true,
- "use_only_cookies": true
- "cookie_lifetime": 0
- "cookie_httponly": true
- "cookie_secure": true
- "use_strict_mode": true
But you can change these values to suit your use case; read more 👀
Returns true if the session is successfully started
Session::start();
This static method is responsible for setting values for $_SESSION
. If the session variable already exists, this method overwrites its value with the new value provided. This method does not return any value, as it simply sets the value of the session variable to $_SESSION
. You can call this method at any time without having initialized the session, we do it for you.
Parameters
- $name (string): The name of the session variable to be set.
- $value (mixed): The value to assign to the session variable.
Session::set('hello', 'Hello World!');
This static method retrieves the values stored in $_SESSION that match the name passed. It may return the value of the superglobal $_SESSION if a value for name is not provided. If the session name does not exist, this method returns the value of $default.
Parameters:
$name
(string|null): The name of the session variable to retrieve. Default isnull
.$default
(mixed): The default value to return if the session variable is not set. Default isnull
.
Session::get('hello')
// return "Hello World!"
Session::get();
/* return [
"hello" => "Hello World!"
]*/
Session::get('unavailable','value');
// return "value"
Session::get('unavailable');
//return null
This static method checks if a specific session exists in $_SESSION
and returns true otherwise it returns false.
Session::has('hello');
// return true
This method deletes a specified session.
Session::remove('hello');
This method returns the current session ID.
Session::id();
// "8r050i4f7mdcc3sikc2mill7ck"
This method generates a new session ID.
Session::renewId();
Session::id();
// "g0d22ie4agrc7fheackc2hsdbc"
This method deletes all session variables and destroys the session.
Session::destroy();
Alias to remove.
Session::delete('hello');
This method sets a flash message in the session cookie, the data stored in the session using this method will be available immediately and during the subsequent HTTP request. After the subsequent HTTP request, the detailed data will be deleted.
Parameters:
$name
(string): The name of the message identifier to set.$value
(mixed): The value to assign to the flash message. Default isnull
.
To store a flash message just call the Session::flash()
method and pass the name and the message.
Session::flash('message','Hello Word!!');
To return the message just call the Session::flash()
method but this time just provide the name.
Note
Remember that Session::flash()
will only return the flash message once after that it is deleted.
echo Session::flash('message');
// Hello Word!!