Integrating CakePHP with PEAR:Auth

If you have a web-site which uses PEAR::Auth, and another section of your site using CakePHP, you’d probably find it baffling that your CakePHP doesn’t remember your authentication cookie. In addition, CakePHP wouldn’t redirect properly after logging in.

The main reason is CakePHP overrides the standard PHP session environment, see cake/lib/session.php


  ini_set('session.use_trans_sid', 0);
  ini_set('session.name', CAKE_SESSION_COOKIE);
  ini_set('session.cookie_lifetime', $this->cookieLifeTime
  ini_set('session.cookie_path', $this->path);
  ini_set('session.gc_probability', 1);

The solution is to change admin/config/core.php


    define('CAKE_SESSION_SAVE', null);

The other problem you’d encounter is because the REQUEST_URI points to the base controller PHP file, and not your current URL. In your controller file, say, app/controller/blogs_controller.php


    function beforeFilter()
    {
         this->checkSession();
    }

    function checkSession()
    {
        require_once("MDB2.php");
        require_once("Auth.php");
        require_once("../../../config.php");
        $auth = new Auth("DB", $dsn, "", false);
        $auth->start();
        if (!$auth->checkAuth())
        {
            $returnURL = $_SERVER['REDIRECT_URL'];
            header("Location: /login.php?return=" . $returnURL);
            exit;
        }
   }

One Response to “Integrating CakePHP with PEAR:Auth”

  1. axpress » Blog Archive » cakePHPでpear::authを使う writes:

    [...] http://www.redmountainsw.com/wordpress/archives/integrating-cakephp-with-pearauth にあった方法で解決したのでメモ。   [...]

Leave a Reply