0

同じマシンに2つのphpWebサイトがあります。最初のサイト(レガシーシステム)には基本認証があります:が設定されているかどうかを確認します$_SESSION['user_id']。私は、最初のサイトの機能を拡張する2番目のサイト(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ファイルにあります...たくさんのファイル。

私のコハナサイトには、アクションの前にセッションをチェックするコントローラーがあります。

<?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");
            }
        }
    }

}

このコードはもうすぐ機能します。あるサイトから別のサイトに移動してユーザーを検出できます...しかし、ユーザーがコハナサイト内のさまざまなアクション間を移動すると、ユーザーテーブルの「ログイン」クーターに気づきました。増加します。つまり、アクションの前に「$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」と一致しませんでした。

その値は、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");
            }
        }
    }

}

それで全部です...

于 2011-06-29T00:20:06.607 に答える