How to use the ci session class? Favorites for use. I have always liked the ci framework, but I have rarely used it recently. In my spare time study, how do I use session classes that stick to ci for everyone? Favorites for use. I have always liked the ci framework, but I have rarely used it recently. Study in your spare time and stick it to everyone to initialize the Session.
Sessions starts to run after each page is loaded. Therefore, the session class must be initialized first. You can initialize it in the controller or automatically load it in the system ). Most session classes run in the background. Therefore, when a session is initialized, its session data is automatically read, created, and updated.
To initialize the session class in your controller constructor, you can use the $ this-> load-> library function:
$ This-> load-> library ('session ');
Once loaded, the session can be used as follows: $ this-> session
How does Sessions work?
After the page is loaded, the session class checks whether the user's cookie contains valid session data. If the session data does not exist (or has expired), a new session is created and saved in the cookie. If the session data exists, its information will be updated and the cookie will be updated at the same time. The session_id value will be re-generated for each update.
You need to know that the session class runs automatically once it is initialized. You can ignore the subsequent things. As you will see below, you can use the session to work normally, or even add your own session data. in all these processes, read, write, and update operations are automatically completed.
What is Session data?
A session is composed of an array containing the following information:
- Unique user Session ID (this is a very robust random string calculated by the average amount of information. it is encrypted using MD5 and is regenerated every five minutes by default.
- User IP address
- User browser information (first 120 characters)
- The latest active timestamp.
The above data will be serialized and stored in the cookie in the following array format:
[Array]
(
'Session _ id' => random hash,
'IP _ address' => 'string-user ip address ',
'User _ agent' => 'string-user agent data ',
'Last _ activity' => timestamp
)
If you enable the encryption settings, the serialized array is encrypted first and then saved to the cookie. This will make the data not easy to see and modify, thus improving security. You can find