首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2个网站之间的共享会话:传统PHP和Kohana 3.1网站

2个网站之间的共享会话:传统PHP和Kohana 3.1网站
EN

Stack Overflow用户
提问于 2011-06-29 00:17:18
回答 1查看 656关注 0票数 0

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

第一个站点(传统站点)检查会话,如下所示:

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

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

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

代码语言:javascript
复制
<?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站点内的不同操作之间导航时,Users表的"logins“计数器会增加。这意味着在执行任何操作之前,"$this->auth->logged_in()“是FALSE...这意味着Auth模块不会在两次操作之间保留我的用户,而是每次都强制登录。

我不知道我能做什么。

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

EN

回答 1

Stack Overflow用户

发布于 2011-06-29 08:20:07

我找到答案了!在Kohana3.1中,Kohana_Session类有一个默认值cookie。

代码语言:javascript
复制
/**
 * @var  string  cookie name
 */
protected $_name = 'session';

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

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

代码语言:javascript
复制
<?php defined('SYSPATH') or die('No direct script access.');

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

我的最后一个控制器是这样的:

代码语言:javascript
复制
<?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");
            }
        }
    }

}

就这样..。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6509682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档