Page 1 of 1

CakePHP: Sessions Rate Topic: -----

#1 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Posted 07 June 2011 - 08:37 AM

This tutorial is only useful if you are using the CakePHP framework.
This tutorial is written for release 1.3.


1. Intro
Sessions are maybe one of the most useful functions of PHP. I think every website which serves something use sessions in some way. This can be used for a user system with login, or a webshop with a shopping cart ... The possibilities are endless.

For this guide you are suspected to already have some knowledge of the working of session, that means, storing session variables, and reading session variables.

CakePHP already creates a session instance for every user that browses the site, this is useful in the way that you don't have to wory about starting or destroying sessions.

So, let's go!

2. Reading session variables
To read session variables, you can simply use the read function, delivered by the session controller.
$this->Session->read('<variable name>')

That's quite simple, isn't it?

If you want, for example, to check whether a user is logged in or not, you can use this code snippet.
$tmp = $this->Session->read('username');
if(!empty($tmp)) //If the user is logged in
{
	$this->redirect('/userpage'); //redirect to a page only users can access
}
else //If not logged in
{
	$this->redirect('/'); //redirect to the homepage
}



3. Writing session variables
The writing process of session variables is as simple as the reading procedure.
$this->Session->write('<variable name>', '<variable value')


That will do the job.

If you want a login procedure for example you can use:
function Login() {
$results = $this->User->findByUsername($this->data['User']['username']); //Search for the username entry in our database
if ($results && $results['User']['password'] == md5($this->data['User']['password'])) //Check if the password from the form matches the md5 password in our db{ 
				
				$this->Session->write('username', $this->data['User']['username']); //Writes the username in a session variable named username
				$this->redirect('/users'); //redirect to the user only page
				}
				else //If the password isn't correct
				{
				$this->redirect('/'); //redirect to the homepage
				}
}



4. Manually destroying sessions
As I already said: the sessions are managed by CakePHP itself. But if you really want to destroy the session, you can use:
$this -> Session -> destroy()



This can be useful for a logout function: (you can just erase the username variable though)
function Logout()
{
if ($this->Session->valid()) //Checks if there is a session active
{
    $this -> Session -> destroy(); //if there is, destroy it
}
    $this -> redirect('/'); //redirect to the homepage
}



5. The end
So, I hope you learned something today, as the official CakePHP cookbook isn't sometimes clear :)

Cya later ;)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1