Home >Backend Development >PHP Tutorial >CakePHP framework Session setting method

CakePHP framework Session setting method

高洛峰
高洛峰Original
2017-02-25 16:25:551489browse

The example in this article describes the CakePHP framework Session setting method. Share it with everyone for your reference, the details are as follows:

CakePHP Session storage options

CakePHP provides users with 3 ways to save Session data:

1. Temporary files in the CakePHP installation directory;
2. Use the default mechanism of PHP;
3. Or serialize to the database.

Corresponding settings In core.php they are:

define('CAKE_SESSION_SAVE', 'php');
# 设置为 'cake',保存session到 /cakedistro/tmp目录
# 设置为 'php',采用PHP的缺省路径
# 设置为 'database',

The PHP mechanism is used by default.

If you choose to use it in the database For storage, you need to create a table in the database. There is a sql script to create the database in /app/config/sql/sessions.sql.

No matter which Session storage method is chosen, the component method operation of CakePHP is basically the same :

CakePHP Session Component is used to interact with Session: including basic Session reading and writing, prompting errors through Session, issuing prompt messages, etc.

Session Component is the default in all Cake controllers Available.

check(string $name);

Check whether there is already a data item with $name as the key value in the Session.

del(string $name);<br>delete(string $name);

Delete the Session variable specified by $name.

error

Returns the most recent error generated by Cake Session Component, often used for debugging.

flash(string $key='flash');

Returns the last message set with setFlash() in Session. If $key is set, the most recently stored message in it will be returned.

read(string $name);

Returns the $name variable value.

renew

By creating a new seesion ID, deleting the original ID, and updating the information in the original Session to the new Session.

setFlash(string $flashMessage, string $layout='default', array $params, string $key='flash');

Set $flashMessage in The information is written to the Session (for subsequent flash() method to obtain).

If $leyout is set to "default", the message is stored as '45eb2251a8971efd8123d0950a23c539'.$flashMessage.'94b3e26ee717c64999d7867364b1b4a3'. If $layout is set to '', the message will be saved as is. If $layout is any other value, the message is saved in the Cake view in the format specified by $layout.

$params parameters will be given functionality in future versions.

$key allows the prompt message to be stored under the key, and flash() reads the message based on the key.

valid returns true when the Session is valid. It is best to use it before the read() operation to determine whether the session you want to access is indeed valid.

write(string $name, mixed $value);

Write the variables $name, $value into the session.

For more articles related to the CakePHP framework Session setting method, please Follow PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn