在两个网站之间共享会话:遗留PHP和Kohana 3.1网站

时间:2011-06-28 16:17:18

标签: php session kohana authentication kohana-auth

我在同一台机器上有2个php网站。第一个站点(遗留系统)具有基本身份验证:检查是否设置为$_SESSION['user_id']。我在第二个站点(基于Kohana 3.1)工作,这将扩展第一个站点的功能。 两个站点都将相互链接,因此我需要在这些系统之间共享会话。两个站点都使用相同的数据库。用户将登录第一个站点。 在我的网站中,我有一个代码可以检测第一个$_SESSION['user_id'],但是我在使用Kohana-Auth模块保留会话时遇到了问题。

第一个站点(旧站点)会像这样检查会话:

<?php
session_start();
if(empty($_SESSION['user_id']))header("Location: index.php?action=3");
... //more dark code

这是所有的php文件......很多文件。

在我的Kohana网站中,我有一个控制器,在任何操作检查会话之前。

<?php

class My_Controller extends Controller_Template {

    public function before() {
        session_start();
        $this->auth = Auth::instance();
        if ($this->auth->logged_in()) {
            //I have session in the second site... Do I have a session on the first one?

            if (!isset($_SESSION['user_id']) || $_SESSION['user_id'] == "") {
                //I have no session in the first site... I logout the user in my site
                $controller = Request::current()->controller();
                if ($controller != 'auth') {
                    Request::current()->redirect('auth/logout');
                }
            }
            $this->user = ORM::factory('user', $this->auth->get_user()->id);
        } else {
            //I have no session in the second site... Do I have a session on the first one?
            $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
            if (isset($user_id)) {
                $user = Model_User::get_user($user_id);
                if ($user->loaded()) {
                    //I have session in the first site... I login the user in my site
                    $this->auth->force_login($user);
                    $this->user = ORM::factory('user', $this->auth->get_user()->id);
                }
            }
            if (!$this->auth->logged_in()) {
                //I still have no session => redirect to login of the first site
                //Request::current()->redirect(...);
                echo Debug::vars("BUUUU");
            }
        }
    }

}

此代码即将开始工作:我可以从一个站点转到另一个站点并检测到用户......但我意识到,当用户在我的Kohana站点内的不同操作之间进行navegates时,“登录” Users表增加。 这意味着在执行任何操作之前,“$this->auth->logged_in()”为FALSE ...这意味着Auth模块不会在操作之间保留我的用户,并且每次都会强制登录。

我不知道该怎么办。

我希望从第一个网站检测会话,但我不想在每次点击时都登录此用户。

1 个答案:

答案 0 :(得分:1)

我找到了答案!! 在Kohana 3.1中,Kohana_Session类具有cookie的默认值。

/**
 * @var  string  cookie name
 */
protected $_name = 'session';

该值与PHP会话的默认名称不匹配:“PHPSESSID”。

通过在config目录中创建名为“session.php”的配置文件来修改该值。所以我创建了一个这样的config / session.php:

<?php defined('SYSPATH') or die('No direct script access.');

return array(
    'native' => array(
        'name' => 'PHPSESSID',
    )
);

我的最终控制器是这样的:

<?php

class My_Controller extends Controller_Template {

    public function before() {
        $this->auth = Auth::instance();
        if ($this->auth->logged_in()) {
            //I have session in the second site... Do I have a session on the first one?

            $user_id = Session::instance()->get('user_id');

            if (!isset($user_id) || $user_id == "") {
                //I have no session in the first site... I logout the user in my site
                $controller = Request::current()->controller();
                if ($controller != 'auth') {
                    Request::current()->redirect('auth/logout');
                }
            }
            $this->user = ORM::factory('user', $this->auth->get_user()->id);
        } else {
            //I have no session in the second site... Do I have a session on the first one?

            $user_id = Session::instance()->get('user_id');

            if (isset($user_id) && $user_id != "") {
                $user = Model_User::get_user($user_id);
                if ($user->loaded()) {
                    //I have session in the first site... I login the user in my site
                    $this->auth->force_login($user);
                    $this->user = ORM::factory('user', $this->auth->get_user()->id);
                }
            }
            if (!$this->auth->logged_in()) {
                //I still have no session => redirect to login of the first site
                //Request::current()->redirect(...);
                echo Debug::vars("BUUUU");
            }
        }
    }

}

这就是......