How to detect session cookies?

680 views
Skip to first unread message

Jaka Certanc

unread,
Jun 12, 2013, 2:59:44 AM6/12/13
to phan...@googlegroups.com
I wrote a simple script that detects cookies that a website sets. Here's the code:

var system = require('system');
var url = system.args[1];
var page = require('webpage').create();
page.open(url, function (status) {
    console.log(JSON.stringify(page.cookies, null, 2));
    phantom.exit();
});


When I execute this from a shell e.g.:
# phantomjs test.js http://www.phantomjs.org

I get a list of cookies that are set at www.phantomjs.org like:

[
  {
    "domain": ".phantomjs.org",
    "expires": "Wed, 11 Dec 2013 18:51:14 GMT",
    "expiry": 1386787874,
    "httponly": false,
    "name": "__utmz",
    "path": "/",
    "secure": false,
    "value": "205055825.1371019874.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"
  },
  {
    "domain": ".phantomjs.org",
    "expires": "Wed, 11 Dec 2013 18:51:14 GMT",
    "expiry": 1386787874,

    "httponly": false,
    "name": "__utmc",
    "path": "/",
    "secure": false,
    "value": "205055825"
  },
  {
    "domain": ".phantomjs.org",
    "expires": "Wed, 12 Jun 2013 07:21:14 GMT",
    "expiry": 1371021674,
    "httponly": false,
    "name": "__utmb",
    "path": "/",
    "secure": false,
    "value": "205055825.1.10.1371019874"
  },
  {
    "domain": ".phantomjs.org",
    "expires": "Fri, 12 Jun 2015 06:51:14 GMT",
    "expiry": 1434091874,
    "httponly": false,
    "name": "__utma",
    "path": "/",
    "secure": false,
    "value": "205055825.1902598942.1371019874.1371019874.1371019874.1"
  }
]


This is correct but if you visit phantomjs.org and take a look at all the cookies that are being set you will see that "__utmc" is a session cookie - it has no expiry date - but phantomjs displays its expiry date as Wed, 11 Dec 2013 18:51:14 GMT (which in my case is 6 months and 12 hours from now). Why?

If I use cookie jar file (e.g. phantomjs --cookies-file=cookies.txt test.js http://www.phantomjs.org) the __utmc cookie is not stored inside (which is correct) but how can I know inside my script that this is a session cookie if phantomjs has its expiry date defined?

I want to strictly separate session cookies from persistent cookies in my script but I am unable to do that since all cookies are shown the same.

Jaka Certanc

unread,
Jun 12, 2013, 3:01:55 AM6/12/13
to phan...@googlegroups.com

update: I ran the same script on a couple of other sites and session cookies always have expiry date defined but not always 6 months and -12 hrs from now - sometimes they are even set to expire in 2 years...

Mauro Parini

unread,
Jan 23, 2015, 11:04:42 AM1/23/15
to phan...@googlegroups.com
Someone has solved or understood the problem?

It's a phantom js bug?

How can I detect session cookie?

Thank You,

Thomas Mallery

unread,
Oct 9, 2015, 11:33:20 PM10/9/15
to phantomjs
Just for future reference in case anyone else stumbles across this thread, it's a bug in PhantomJS where it converts cookies to JSON strings. Issue: https://github.com/ariya/phantomjs/issues/13638.

Tom
Reply all
Reply to author
Forward
0 new messages