Web Development

As every web developer knows, HTTP is stateless.

In plain PHP you would need to write something like session_start() and access session variables via the session global variable, $_SESSION.

Start the Session

session_start();

Setting values to the Session variable

$_SESSION['username'] = 'John Smith';

Accessing the values in the Session variable

$username = $_SESSION['username'];

When using Yii, this could be enabled by setting Yii’s session to autoStart. You won’t need to access the $_SESSION variable but instead use Yii::app()->session.

Setting values to the Session variable

Yii::app()->session['username'] = 'John Smith';

Accessing the values in the Session variable

Yii::app()->session['username'];

The Yii Framework allows you to use convenience methods to access the session.

Setting values to the Session variable

Yii::app()->session->add('username', 'John Smith');

Accessing the values in the Session variable

Yii::app()->session->itemAt('username');