codeigniter nodejs 和 nowjs 集成
我正在尝试将 nodejs 和 nowjs 与 codeigniter 框架集成,因为我没有时间重写整个站点,所以我想检查用户是否登录。我正在使用 ion_auth 并将会话存储在数据库中。
客户端js:
var session_id = $.cookie('sc_session');
$("form").submit(function() {
now.distributeMessage($("textarea").val(),session_id);
});
now.receiveMessage = function(message){
$("body").append("<br>" + message);
};
这是nodejs服务器代码:
编辑:*稍微简化一下,从客户端js获取会话cookie*
everyone.now.distributeMessage = function(message,session_cookie) {
exec('php index.php user_session decrypt ' + encodeURIComponent(session_cookie),
function (error, stdout, stderr) {
var parts = stdout.split(';');
var session_id = parts[1].split(':')[2];
var query = 'select * from sc_sessions where session_id=' + session_id;
client.query(query, function (err, results, fields) {
if (results) {
everyone.now.receiveMessage(str);
}
});
});
};
这是由nodejs调用的控制器:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_session extends CI_Controller
{
public function __construct()
{
parent::__construct();
if (!$this->input->is_cli_request()) {
redirect('index');
}
}
public function decrypt($session_id)
{
$this->load->library('encrypt');
$session_id = urldecode($session_id);
echo $this->encrypt->decode($session_id);
}
}
它正在工作,我可以从数据库中的会话将user_data记录到控制台,并且它正确调用everyone.now.receiveMessage函数。
我对此有三个问题:
1)有一些行插入到 sc_sessions 数据库表中,其中包含 session_id,但 user_data、user_agent 和 ip 为 0.0.0.0 为空。每次提交表单时一行。
造成这种情况的原因是什么?我该如何解决?
我知道 ajaxcalls 和 codeigniter 会话存在问题。请参阅此主题:
有没有办法查看请求是否是通过 websockets 发出的?
2)检查用户是否登录的最佳方法是什么?例如 if 语句,您检查查询是否返回任何内容或者是否有更好的方法?
3)有更好的方法吗?
任何帮助都很感激,因为我被困在这个问题上。
乔治
编辑: 从命令行调用脚本导致它创建一个新会话。通过扩展会话类来防止它。
应用程序/库/MY_Session.php
<?php
class MY_Session extends CI_Session {
public function __construct()
{
$CI = get_instance();
if ($CI->input->is_cli_request())
{
return;
}
parent::__construct();
}
}
I am trying to integrate nodejs and nowjs with codeigniter framework, As I dont have time to rewrite the whole site I want to check if a user is logged in. I am using ion_auth and I am storing the session in db.
Client js:
var session_id = $.cookie('sc_session');
$("form").submit(function() {
now.distributeMessage($("textarea").val(),session_id);
});
now.receiveMessage = function(message){
$("body").append("<br>" + message);
};
Heres the nodejs server code:
EDIT:*Simplified it a little, and get session cookie from client js*
everyone.now.distributeMessage = function(message,session_cookie) {
exec('php index.php user_session decrypt ' + encodeURIComponent(session_cookie),
function (error, stdout, stderr) {
var parts = stdout.split(';');
var session_id = parts[1].split(':')[2];
var query = 'select * from sc_sessions where session_id=' + session_id;
client.query(query, function (err, results, fields) {
if (results) {
everyone.now.receiveMessage(str);
}
});
});
};
And here is the controller called by nodejs:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_session extends CI_Controller
{
public function __construct()
{
parent::__construct();
if (!$this->input->is_cli_request()) {
redirect('index');
}
}
public function decrypt($session_id)
{
$this->load->library('encrypt');
$session_id = urldecode($session_id);
echo $this->encrypt->decode($session_id);
}
}
It is working and i can log the user_data to the console from the session in database, and it calls the everyone.now.receiveMessage function properly.
I have three questions regarding this:
1) there are rows inserted into the sc_sessions db table with a session_id but with empty user_data, user_agent and ip 0.0.0.0. One row each time the form is submitted.
What is causing this and how do i fix it?
I know that there is a problem with ajaxcalls and codeigniter sessions. See this thread:
Is there a way to see if the request was made with websockets?
2) What is the best way to check if the user is logged in? e.g if statement where you check to see if the query returned anything or is there a better method?
3) Is there a better method of doing this?
Any help appreciated as I am stuck on this.
George
EDIT:
Calling the script from commandline caused it to create a new session. Prevent it by extending the session class.
application/libraries/MY_Session.php
<?php
class MY_Session extends CI_Session {
public function __construct()
{
$CI = get_instance();
if ($CI->input->is_cli_request())
{
return;
}
parent::__construct();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很好的问题,但是测试答案需要很多开销。
我最好的猜测是 CLI 不会传递 user_agent 和 ip 等环境变量。这些由 apache 处理,与本机 CLI 请求无关。如果您知道它在节点环境中是安全的,我会单独使用 session_id 。
Great question, but lots of overhead to test an answer.
My best guess is CLI doesn't pass environmental variables like user_agent and ip. Those are handled by apache, and aren't relevant in CLI requests natively. I'd go by session_id alone if you know it's secure in a node environment.