0

我在同一台机器上有 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 站点内的不同操作之间导航时,用户表的“登录”计算机增加。这意味着在任何操作之前,“ $this->auth->logged_in()”是FALSE......这意味着 Auth 模块不会在操作之间保留我的用户并且每次都强制登录。

我不知道我能做什么。

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

4

1 回答 1

1

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

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

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

通过在配置目录中创建一个名为“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");
            }
        }
    }

}

就这样...

于 2011-06-29T00:20:06.607 回答