我有两个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站点内的不同操作之间导航时,Users表的"logins“计数器会增加。这意味着在执行任何操作之前,"$this->auth->logged_in()“是FALSE...这意味着Auth模块不会在两次操作之间保留我的用户,而是每次都强制登录。
我不知道我能做什么。
我想从第一个站点检测会话,但我不想在每次点击时都登录这个用户。
发布于 2011-06-29 08:20:07
我找到答案了!在Kohana3.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");
}
}
}
}就这样..。
https://stackoverflow.com/questions/6509682
复制相似问题