Joke Collection Website - Blessing messages - Php CI framework, how to use sessions

Php CI framework, how to use sessions

There are two ways to use a session:

1 is the original session usage method of php. This is very simple, $_SESSION['name']="name ",and then display it where necessary: echo $ _ session ['name'];

2 is a method of codeigniter framework:

The following details how to use this somewhat complicated method:

First, it is found in the config.php file under \ ci \ application \ config: $ config ['encryption _ key'] =''; You can fill in any value here, but it cannot be empty. Generally speaking, it's English. Don't get into trouble.

Then find it in the auto.php file under \ci\application\config: $ autoload ['libraries'] = array ('); Fill in: $ autoload ['libraries'] = array ('session'); Or write: $ this-& gt;; Load->; Library ("session"); This will do.

Now that the environment is configured, it's time to write code:

Write down where you need to arrange the meeting:

$ this-& gt; Session->; set_userdata('name ',' yang ');

So there is a value in the conversation.

Display value:

echo $ this-& gt; Session->; user data(' name ');

If it is an array, then:

$newdata = array(

User name' => Jane Doe,

E-mail' = & gtjohndoe@some-site.com',

Logged _ in' => Real

);

$ this-& gt; Session->; set _ user data($ new data);

The following is someone else's detailed knowledge related to nonsense:

After each page is loaded, the session will start running, so the session class must be initialized first.

1. can be initialized in the controller, or $ autoload ['libraries'] = array ('session') can be automatically loaded in the system;

2. To initialize the session class in the controller constructor, you can use $ this->; Load->; Library function: $ this-& gt;; Load->; Library ("session"); After loading, the session can be used as follows: $ this->; Conversation.

Most session classes will run in the background, so when a session is initialized, its session data will be automatically read, created and updated.

How does a session work?

It is very important to know that once the session class is initialized, it will run automatically. For the latter things, it can be completely ignored. As you will see below, you can use session to work normally, and even add your own session data, and in the process of all this, reading, writing and updating operations are automatically completed.

When the page is loaded, the session class will check whether there is valid session data in the user's cookie. If the session data does not exist (or has expired), a new session will be created and saved in a cookie. If the session data exists, his information will be updated, and the cookie will also be updated. The value of session_id is regenerated with each update.

By default, the session Cookie is updated every 5 minutes, which will reduce the load on the processor. If you load the page repeatedly, you will find that the "last activity" time will change within five minutes, or more than five minutes, that is, the time when the cookie was last written. This time can be changed by setting the $config['sess_time_to_update'] line in the application/config/config.php file.