12

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.

redbeam_
  • 337
  • 2
  • 13
razorxan
  • 516
  • 1
  • 7
  • 20
  • I'm also experiencing the same problem. I have tried the --cookies-file parameter as well, I can see that the login action got executed correctly, but when it redirect to the next page, the user was not logged in. Definitely a session problem. looking forward for the solution. – Adrian Liem Sep 13 '13 at 07:03
  • Did you find a solution to that problem ? Still struggling and can't find anything to avoid compromise data security. – CinetiK Dec 17 '13 at 15:41

2 Answers2

8

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 ?

counterbeing
  • 2,721
  • 2
  • 27
  • 47
Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
  • Yes it does but I still can't keep myself logged in though. I also tried to delete the old cookie.txt file, launching the script again and see if cookie data is being correctly written on it and it seems to. – razorxan Feb 12 '13 at 13:55
  • @razorxan so your problem seems to be solved? if it is please mark the above question as correct.. so that we don't leave this open ended – abbood Apr 06 '13 at 04:34
  • 2
    @abbood no, his problem is not solved. Cookie data is being written correctly but he still loses his login session (like me now...) – alumi Jun 29 '15 at 08:03
8

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.

BlakeH
  • 3,354
  • 2
  • 21
  • 31
  • 1
    This is a known issue apparently: see https://github.com/ariya/phantomjs/issues/12277 and http://stackoverflow.com/a/24184040/308421 – liorda Nov 23 '15 at 21:21
  • 1
    Here is a gist to do this (although the option -cookies-file seems to be working for non-session cookies) : [https://gist.github.com/jarnix/8d6e279d7571da7fe3227f1e103f5562](https://gist.github.com/jarnix/8d6e279d7571da7fe3227f1e103f5562) – Julien Ricard Jan 17 '17 at 17:45