Yii 1.1: How to validate CSRF token with session


How Yii Validate CSRF Token

First of all, You must change component config to enable the default Yii CSRF validation.

'components' => array(
    'request' => array(
        'enableCsrfValidation' => true,

Note: When you ebable CSRF validation and use form builder to generate a form(only post), Yii will auto generate a hidden field and put it in the form, at the same time, Yii will create a cookie with CSRF token. When you submit the form, Yii will compare two CSRF tokens from post and cookie.

What Problem Will Happen

1.The user client DOES NOT accept cookie.

2.The user client CAN NOT send a request with cookie.

For example: upload file use flash (swfupload)

How To Solve

A good solution is to use session instead of cookie.

Do it like this

1.First, You must use your own HttpRequest class instead of Yii built-in

Create a new class file HttpRequest extends CHttpRequest in path/to/protected/components

We need override two methods

private $_csrfToken;
public function getCsrfToken()
        $session = Yii::app()->session;
            $csrfToken = sha1(uniqid(mt_rand(),true));
            $session->add($this->csrfTokenName, $csrfToken);
        $this->_csrfToken = $csrfToken;
    return $this->_csrfToken;
public function validateCsrfToken($event)
        // only validate POST requests
        if($session->contains($this->csrfTokenName) && isset($_POST[$this->csrfTokenName]))
            throw new CHttpException(400,Yii::t('yii','The CSRF token could not be verified.'));

2.Change the component config to use the HttpRequest class

'components' => array(
    'request' => array(
        'class' => 'application.components.HttpRequest',
        'enableCsrfValidation' => true,

Some Tips

If user does nothing too long time, the session will be removed by session gc. In that case, CSRF validation will raise a 400 HTTP exception.

The default session timeout in php5 is 1440(may be not exact), your can use function ini_get('session.gc_maxlifetime') to view the default timeout and use the function ini_set('session.gc_maxlifetime', $timeout) to set.

In Yii, We hava a easy way to set. Just change the session component config.

'components' => array(
    'session' => array(
        'timeout' => 86400,

Total 2 comments

#19909 report it
benelli at 2016/07/18 06:09am
Yes, It does work.

@16972 probably made a mistake in the name declaration of the class. You simply have to declare the new class like this:

class HttpRequest extends CHttpRequest {
    // here the code from the post.

I'm testing it and it works prefectly.

#16972 report it
santosh joshi at 2014/04/17 09:30am
It, does not work

I have get error

Fatal error: Call to undefined method CHttpRequest::init() in E:\xampp\yii-framework\framework\base\CModule.php on line 387

Please help me

Leave a comment

Please to leave your comment.

Write new article
  • Written by: yangmls
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +23 / -1
  • Viewed: 57,696 times
  • Created on: Nov 21, 2011
  • Last updated: Nov 21, 2011
  • Tags: CSRF, session