2

I'm trying to parse a CodeIgniter cookie with node.js. I'm using this code to get all the cookies:

function parse_cookies(_cookies) {
   var cookies = {};

   _cookies && _cookies.split(';').forEach(function( cookie ) {
     var parts = cookie.split('=');
     cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim();
   });

   return cookies;
}

It returns all the cookies so then I do:

var cookies = parse_cookies(handshakeData.headers.cookie);
console.log(cookies.ci_session);

But the ci_session cookie is in this format:

a:4:{s:10:"session_id";s:32:"152933dbf8a52a55b48518b940451aey";s:10:"ip_address";s:12:"192.168.1.68";s:10:"user_agent";s:72:"Mozilla/5.0+(Windows+NT+6.1;+WOW64;+rv:34.0)+Gecko/20100101+Firefox/34.0";s:13:"last_activity";i:1419314157;}44503444ab1688f1e3a5ece6a3ff1e024e4b773f

How can I get the session_id value from it?

1 Answer 1

1

This format looks like PHP's serialize() function. There's an NPM Package to parse those.

If you don't want to add another dependency, and assuming CodeIgniter's cookie scheme won't change much, you can do something as simple as

cookies.ci_session.match(/s:32:"([a-z0-9]{32})"/)[1]

Which finds the occurrence of s:32:"(??)" where ?? is 32 alphanumeric characters. The [1] is because match() returns an array of matches, [0] is the entire match (s:32:"(??)") and [1] is the first capture group (enclosed in brackets), namely ??, which is what you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.