【问题标题】:Share session between 2 websites: a legacy PHP and a Kohana 3.1 site在 2 个网站之间共享会话:旧版 PHP 和 Kohana 3.1 网站
【发布时间】:2011-06-28 16:17:17
【问题描述】:

我在同一台机器上有 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-&gt;auth-&gt;logged_in()”是 FALSE... 这意味着 Auth 模块不会在操作之间保留我的用户并且每次都强制登录。

我不知道我能做什么。

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

【问题讨论】:

    标签: php session kohana authentication kohana-auth


    【解决方案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");
                }
            }
        }
    
    }
    

    就是这样……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2017-09-13
      • 2015-08-02
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多