[CakePHP]: Session is and how to use

PHP CAKE PHP

Posted on 2015-11-12 11:02:36


is an important concept in programming PHP Net. It is used to manage the session of the user as the management mechanism of user authentication (login), manage the flow of information to remember, ... And of course CakePHPFramework for us also in providing library to handle session .
In this article, I will guide you how to use the Session in CakePHP with examples of usage in each case
In application programmers want, then we should also have the important components such as Helper and Component via two variable $ helpers and $ component
If you do not know how to write a component for CakePHP helper and please review the article 2: How to write a helper in CakePHP Framework andCakePHP Framework component in writing .
For example, declare:
1
2
var  $helpers = array('Html', 'Form','Javascript','Ajax','Common');
var  $components = array('Acl',Mail);

Having these ingredients be declared only in $ helpers as: Html, Form ..., $ component as: Acl, Mail ... However Session is an exception, it may be declared in both components $ helpers and $ components

1
2
var  $helpers = array('Html', 'Form','Javascript','Ajax','Common',Session);
var  $components = array('Acl',Mail,Session);
 

The syntax to use: Whether Session declared at $ helpers and $ component matter, it also has the following main functions: a) write ($ name, $ value) - Save session worth $ value on name $ name - the $ name may use the dot (.) to form an array session For example:

 
2
3
4
5
6
//Save to session with basic syntax
$this->Session->write('Username','administrator');
//Save with array
$this->Session->write('Person.name', 'admin');
$this->Session->write('Person.email', 'admin@yahoo.com');
 
b) read($name)
- Get the value of a session by name Example:
2
3
4
5
6
7
8
9
10
echo $this->Session->read('Username');
//Result
admin
pr($this->Session->read('Person'));
//Result
Array
(
    [name] => admin
    [email] => admin@yahoo.com
c) check($name)- Check that there exist session called $ name or not Examples: nonexistent Session Username
d) delete ($ key) - Clear Session by name
e) destroy () - Delete all cookies and Session
f) error () - Determine the final error to the next session
g) setFlash ($ message, $ element = 'default', $ params = array (), $ key = 'flash')
- Commonly used in Controllers
+ $ Message: the content displayed + $ element: load files contain .
Noi around $ message content stored in file folders * .ctp app / views / elements / + $ param: used to change the properties calculated as: class, id, style .. + $ key: The default value is "flash"
Example : I have a file sms.ctp (app/views/elements/sms.ctp)
Execute the command:
Then file contents sms.ctp will be loaded and displayed as: Thanks