Reputation: 187
We are unable to get ci_session in React with a Node.js application from the CodeIgniter application. We currently have a running CodeIgniter portal and want to revamp a single page with React and Node.js. The page renders correctly, but we can't retrieve the ci_session."
Let me know if you'd like further adjustments
Upvotes: 0
Views: 13
Reputation: 57
Below is one complete working solution that uses Redis as a common session store and a minimal shared payload (for example, a user ID) so that both applications can “read” the same session. In this setup, CodeIgniter writes its session data into Redis without encrypting the payload, and your ExpressJS app retrieves and decodes that data (using a PHP serializer library) so you can access values such as the user ID.
Important:
– Both apps must be served under a common domain (or subdomains) so that the cookie (named “ci_session” in this example) is sent to both.
– CodeIgniter must be configured to avoid encrypting its session data (or at least keep the data in a simple format), because Express needs to unserialize it.
– The session data should be kept minimal (e.g. a user identifier) to avoid cross-framework serialization issues.
Edit your application/config/config.php
(for CodeIgniter 3) so that sessions are stored in Redis. For example:
// Use Redis as session driver
$config['sess_driver'] = 'redis';
$config['sess_save_path'] = 'tcp://127.0.0.1:6379';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200; // Session expiration in seconds (e.g., 2 hours)
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
// Ensure cookies are available to both apps
$config['cookie_domain'] = '.example.com'; // Use your domain
// Make sure the session data is not encrypted so Express can read it.
$config['sess_encrypt_cookie'] = FALSE;
When a user logs in (or at any appropriate point), store a minimal value such as the user ID:
// Example in one of your controllers
$this->session->set_userdata('user_id', $user_id);
This way the session stored in Redis will be a PHP-serialized array (or similar format) that Express can parse.
In your Express project, install the required packages:
npm install express express-session connect-redis redis php-serialize
Create your Express app so that it reads the same session cookie and store:
const express = require('express');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const redis = require('redis');
const { unserialize } = require('php-serialize'); // This library decodes PHP-serialized strings
const app = express();
const redisClient = redis.createClient({ host: '127.0.0.1', port: 6379 });
// Configure express-session to use Redis and the same cookie name
app.use(session({
name: 'ci_session',
store: new RedisStore({ client: redisClient }),
secret: 'your_express_secret', // Only used to sign the cookie in Express
resave: false,
saveUninitialized: false,
cookie: {
domain: '.example.com', // Must match CodeIgniter's cookie_domain
maxAge: 7200000, // 2 hours in milliseconds
secure: false // Set true if using HTTPS
}
}));
// Middleware: Retrieve CodeIgniter session data from Redis and decode it
app.use((req, res, next) => {
// CodeIgniter stores session data in Redis with a key like "ci_session:<session_id>"
const key = 'ci_session:' + req.sessionID;
redisClient.get(key, (err, data) => {
if (err) return next(err);
if (data) {
try {
// Unserialize PHP serialized session data into a JavaScript object
req.ciSession = unserialize(data);
} catch (e) {
return next(e);
}
}
next();
});
});
// Example route that uses the shared session data
app.get('/', (req, res) => {
if (req.ciSession && req.ciSession.user_id) {
res.send('User ID from CodeIgniter session: ' + req.ciSession.user_id);
} else {
res.send('No user data in session.');
}
});
app.listen(3000, () => {
console.log('Express server running on port 3000');
});
.example.com
(replace with your actual domain), the browser sends the same ci_session
cookie to both the CodeIgniter and Express apps.php-serialize
library to convert it to a JavaScript object, making values (like user_id
) available for use.This complete solution ensures that a user logged in via CodeIgniter will have their session (and minimal user data) available to your ExpressJS application, making cross-framework session sharing work seamlessly.
By following these steps and code samples, you’ll have a working solution to share sessions between a CodeIgniter application and an ExpressJS application.
Upvotes: 0