esr-php-session
v1.0.5
Published
esr-php-session is a session for express, socket.io, php which doesn't suck to work together with Redis as a database.
Downloads
9
Maintainers
Readme
esr-php-session
A [esr-php-session][] is a session for express
, socket.io
, php
, which doesn't suck to work together with Redis
as a database.
Authors: Neo he is very well known in runet as Hacker Kselax Here is his [home page] to visit
[![npm version][npm-badge]][npm] [![dependency status][dep-badge]][dep-status] [esr-php-session]: https://www.npmjs.com/package/esr-php-session [npm]: https://www.npmjs.org/package/esr-php-session [npm-badge]: https://img.shields.io/npm/v/esr-php-session.svg?style=flat-square [dep-status]: https://david-dm.org/ericf/esr-php-session [dep-badge]: https://img.shields.io/david/ericf/esr-php-session.svg?style=flat-square [home page]: https://kselax.ru/en/
Goals & Design
I built this package out of frustrations of existing packages express-session
, connect-redis
, express-socket.io-session
. This three doesn't work properly together with redis database. and We can't find the package that allows working with php.
So this simple package was designed to work with sessions using Redis as database and three of express + socket.io + php. You can use them all together or for each separately
Installation
$ npm install esr-php-session --save
Before usage important to know
For session works properly with php+socket.io+express you have to specify the same name in any initialization. This option esr_php_session_name: 'my_session',
should be equal otherwise they can't work simultaneously and to know each other
Usage with express and socket.io
Include to file
var esr_php_session = require('esr-php-session');
in middlewire add this lines
For using with express
app.use(esr_php_session.express_session({
esr_php_session_name: 'my_session',
secret: 'my new secret',
maxAge: (60 * 60 * 24), // by default 24 hours
ex: 60,
}));
then you can put variables to res.esr_php_session
something like res.esr_php_session = some_value
and this value will saved and will available on next reload page.
For using with socket.io
io.use(esr_php_session.socket_io_session({
esr_php_session_name: 'my_session',
secret: 'my new secret',
maxAge: (60 * 60 * 24), // by default 24 hours
ex: 60,
}));
now for you available socket.esr_php_session
and you can put there some variable like socket.esr_php_session.views = 30
Usage with php
at first find file /node_modules/esr-php-session/Esr_php_session.php
and include it to your php project
require_once 'Esr_php_session.php';
then create an object
$obj = new Esr_php_session(array(
'esr_php_session_name' => 'my_session',
'secret' => 'my new secret',
'maxAge' => (60 * 60 * 24), // by default 24 hours
'ex' => 60,
));
that's it, when you create an object in this time will be automatically added to $_COOKIE
a new element with name of your session $_COOKIE[esr_php_session_name]
Example with express
const express = require('express');
const esr_php_session = require('esr_php_session');
var app = express();
app.listen(3000);
app.use(esr_php_session.express_session({
esr_php_session_name: 'my_session',
secret: 'my new secret',
maxAge: (60 * 60 * 24), // by default 24 hours
ex: 60,
}));
app.get('/', function(req, res){
console.log(req.esr_php_session.views);
if(req.esr_php_session.views || req.esr_php_session.views == 0){
req.esr_php_session.views++;
}else{
req.esr_php_session.views = 0;
}
console.log('req.esr_php_session.views = ', req.esr_php_session.views);
res.send('Views = ' + req.esr_php_session.views);
});
Example with socket.io
...
io.use(esr_php_session.socket_io_session({
esr_php_session_name: 'my_session',
secret: 'my new secret',
maxAge: (60 * 60 * 24), // by default 24 hours
ex: 60,
}));
io.on('connection', function(socket){
console.log('connected socket.id = ' + socket.id);
if(socket.esr_php_session){
if(socket.esr_php_session.views){
socket.emit('message', socket.esr_php_session.views);
}else{
socket.esr_php_session.views = 0;
socket.emit('message', socket.esr_php_session.views);
}
}
console.log(socket.esr_php_session.views);
}
More examples
See on github in folder examples or inside package in /node_modules/esr-php-session/examples
License
This software is free to use under GNU General Public License GPL. See the license description for license text and copyright information.