我試圖使用KCFinder和CKEditor和Kohana。我需要每個用戶訪問他們自己的單獨上傳文件夾。爲此,我想將當前用戶ID傳遞給KCFinder。爲了安全地做到這一點,我不能通過URL作爲GET變量傳遞它,所以我需要通過會話來完成。在Kohana外訪問Kohana中設置的會話變量
我試着在Kohana中設置變量,當我在Kohana中使用var_dump($_SESSION)
時,變量就在那裏。當我在KCFinder中這樣做時,它不在那裏。我檢查了session_id
,發現它在頁面間發生變化,所以我將session_id
作爲GET變量通過URL傳遞給KCFinder,然後將session_id
設置爲KCFinder的匹配。但是變量是仍然不適用於當我var_dump($_SESSION)
。
這裏是我的Kohana行動:
public function action_edit()
{
$this->template->javascripts[] = '/ckeditor/ckeditor.js';
$_SESSION['KCFINDER']['user_id'] = Auth::instance()->get('id');
var_dump(session_id(), $_SESSION['KCFINDER']);
$id = (int)$this->request->param('id');
$this->layout = Request::factory('document/update/'.$id)->execute()->body();
$this->template->js_custom .= "var phpsessionid = '".session_id()."';";
}
// document/update action
public function action_update()
{
$id = (int)$this->request->param('id');
if (! $id)
{
throw new HTTP_Exception_400('No document ID was specified. Please go back and try again.');
}
$document = ORM::factory('Document', $id);
if (! $document->loaded())
{
throw new HTTP_Exception_400('No valid document ID was specified.');
}
$layout = View::factory('layouts/documents/edit')
->bind('back_link', $back_link)
->bind('values', $values)
->bind('errors', $errors)
->bind('success', $success)
->bind('is_acp', $is_acp);
$is_acp = (strpos(Request::initial()->uri(), 'acp') !== FALSE);
if (! $is_acp AND Auth::instance()->get('id') != $document->user_id)
{
throw new HTTP_Exception_403('Unauthorised access. You do not have permission to edit this document.');
}
elseif ($document->is_published AND ! Auth::instance()->get('is_admin'))
{
throw new HTTP_Exception_403('Unauthorised access. You cannot edit a published document.');
}
$back_link = $is_acp ? '/acp/documents' : '/account';
$values = array();
$errors = array();
foreach ($document->table_columns() as $key => $null)
{
$values[$key] = $document->$key;
}
if (Request::initial()->method() == Request::POST)
{
// We assume that is_published and is_paid are unchecked. Later on we check to see if they are checked and change the values.
if ($document->is_published == 1 AND $is_acp)
{
$values['is_published'] = -1;
}
if ($document->is_paid == 1 AND $is_acp)
{
$values['is_paid'] = 0;
}
foreach (Request::initial()->post() as $key => $val)
{
if ($key == 'is_published')
{
// Check for a first time publish, and if it is run the publish method to save the PDF.
if ($document->is_published == 0)
{
Request::factory('document/publish/'.$document->id)->query(array('no_redirect' => TRUE))->execute();
}
$values[$key] = 1;
}
elseif ($key == 'is_paid')
{
$values[$key] = 1;
}
else
{
$values[$key] = $val;
}
}
$document->values(Request::initial()->post(), array('title', 'summary', 'category_id', 'content'));
if ($is_acp)
{
$document->is_published = $values['is_published'];
if ($document->is_published == 1)
{
$document->date_published = time();
}
$document->is_paid = $values['is_paid'];
}
try
{
$document->save();
$success = TRUE;
}
catch (ORM_Validation_Exception $e)
{
$errors = $e->errors('models');
}
catch (Exception $e)
{
throw new HTTP_Exception_500($e->getMessage);
}
}
$this->response->body($layout->render());
}
這是我KCFinder配置的頂部:
$session_id = @$_GET['phpsession'];
session_start();
session_id($session_id);
define('ROOT', str_replace('eshop/kcfinder', '', __DIR__), TRUE);
var_dump(session_id(), $_SESSION); exit;
當我打開KCFinder會話是空的(或具有隻是KCFinder默認會話)並且對Kohana會議沒有影響。任何人都可以幫我解決這個問題嗎?
[如果指定了'id',則會替換當前的會話ID。 'session_id()'需要在'session_start()'之前被調用。](http://php.net/manual/en/function.session-id.php) – CBroe 2015-02-11 10:09:25
><我不敢相信這很簡單。非常感謝你,自從昨天上午以來,我一直在爲此苦惱... – Styphon 2015-02-11 10:11:16
好吧,我添加了這個答案,所以你將有一些東西可以接受:-) – CBroe 2015-02-11 10:38:14