1.

How To Read , Write And Delete Sessions In Phalcon?

Answer»

Starting the Session: Some APPLICATIONS are session-intensive, almost any action that performs requires access to session data. There are others who access session data casually. Thanks to the service container, we can ENSURE that the session is accessed only when it’s clearly needed:

<?php

use PhalconSessionAdapterFiles as Session;

// Start the session the FIRST TIME when some component request the session service
$di-&GT;setShared(
"session",
function () {
$session = new Session();

$session->start();

return $session;
}
);

Storing/Retrieving data in Session: From a controller, a view or any other component that extends PhalconDiInjectable you can access the session service and store items and retrieve them in the following way:

<?php

use PhalconMvcController;

class UserController extends Controller
{
public function indexAction()
{
// Set a session variable
$this->session->set("user-name", "Michael");
}

public function welcomeAction()
{
// Check if the variable is defined
if ($this->session->has("user-name")) {
// Retrieve its value
$name = $this->session->get("user-name");
}
}

}

Removing/Destroying Sessions: It’s also possible remove specific variables or destroy the whole session:

<?php
use PhalconMvcController;

class UserController extends Controller
{
public function removeAction()
{
// Remove a session variable
$this->session->remove("user-name");
}

public function logoutAction()
{
// Destroy the whole session
$this->session->destroy();
}
}

Starting the Session: Some applications are session-intensive, almost any action that performs requires access to session data. There are others who access session data casually. Thanks to the service container, we can ensure that the session is accessed only when it’s clearly needed:

<?php

use PhalconSessionAdapterFiles as Session;

// Start the session the first time when some component request the session service
$di->setShared(
"session",
function () {
$session = new Session();

$session->start();

return $session;
}
);

Storing/Retrieving data in Session: From a controller, a view or any other component that extends PhalconDiInjectable you can access the session service and store items and retrieve them in the following way:

<?php

use PhalconMvcController;

class UserController extends Controller
{
public function indexAction()
{
// Set a session variable
$this->session->set("user-name", "Michael");
}

public function welcomeAction()
{
// Check if the variable is defined
if ($this->session->has("user-name")) {
// Retrieve its value
$name = $this->session->get("user-name");
}
}

}

Removing/Destroying Sessions: It’s also possible remove specific variables or destroy the whole session:

<?php
use PhalconMvcController;

class UserController extends Controller
{
public function removeAction()
{
// Remove a session variable
$this->session->remove("user-name");
}

public function logoutAction()
{
// Destroy the whole session
$this->session->destroy();
}
}



Discussion

No Comment Found