Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phantomjs and sessions

I can't maintain session surfing through a website once logged in.

I do can successfully login on the site (i specify that whatever the page is, after the login you will be redirected to the homepage) but then I have to move to another page. First I tried with page.open() then with page.evaluate changing the location.href window property, but in both cases unfortunately the result is that I'm not logged in anymore. I traced the login status just rendering the page on every page load event with incremental png names (1.png, 2.png, etc) . I also tried with --cookies-file=cookies.txt param but it didn't help much.

My questions are: What is the best way to "move" through site pages with phantomjs? Is there a specific way to handle sessions in these cases (maybe sending cookies manually on each .open(), just saying)?

Thanks for help.

like image 314
razorxan Avatar asked Feb 12 '13 03:02

razorxan


2 Answers

Sessions require cookies. You have to add an extra argument in phantomjs.

--cookies-file=/path/to/cookies.txt

Look here for more info.

Edit : Does your cookies.txt contains something ?

like image 142
Cybermaxs Avatar answered Nov 06 '22 06:11

Cybermaxs


I had issues with the cookies file approach. The file would be written, and I could see "cookie information" in the file, but future requests would be interpreted as unauthenticated. As a last ditch effort, I simply took the page.cookies array, serialized it to JSON and saved it to a file. My next script would open the file, deserialize it to a variable and set the page.cookies to the variable. Sure enough this worked! Just thought I would pass it along.

like image 31
BlakeH Avatar answered Nov 06 '22 05:11

BlakeH