duan3019 2016-10-31 16:54 采纳率: 100%
浏览 111

casperjs持久登录

I am running Phantomjs, Casperjs with php-casperjs

I want that the login persist on a website, I don't want to have to login every time I start casper.

Now I tried with many sites, I enabled cookies and tested, they works and are the same with different session.

But still the login doesn't persist.

I tried do use disk-cache true. Nothing change.

The Casper UserAgents, viewPortWidth/height doesn't change. The IP is the same. So a fingerprint authentication should still work...

What it is missing?

Edit: some code:

// Casper constructor and setting:

    private $_userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36';
    private $_viewPortWidth = 1345;
    private $_viewPortHeight = 749;
    private $_temp_dir = '/tmp';
    private $_path2casper = '/usr/local/bin/'; //path to CasperJS

//

    $this->casper->setOptions(array(
        'ignore-ssl-errors' => 'yes',
        'ssl-protocol' => 'any',
        'cookies-file' => sys_get_temp_dir().'/JScookies.txt',
        'load-images' => 'true',
        'disk-cache' => 'true'
    ));

// Login

    $this->casper->start($this->LINK_LOGIN);

    $this->casper->waitForSelector('input#btnLogin', 3000);
    $this->casper->fillForm(
        'form[action="/accedi"]',
        array(
            'Username' => $this->Account,
            'Password' => $this->Password
        ), true);
    $this->casper->wait(1000);

    $this->casper->run();
    if($this->CheckLogin($this->casper->getCurrentPageContent()))
        echo "<br> <b>Login Success</b>";
    else
        echo "<br> <b>Error Login</b>";

// test after login

    $this->casper->start($this->LINK_MAIN_PAGE);

    $this->casper->wait(2000);
    $this->casper->run();

    if($this->CheckLogin($this->casper->getCurrentPageContent()))
        echo "<br> <b>Login Success</b>";
    else
        echo "<br> <b>Error Login</b>";
  • 写回答

1条回答 默认 最新

  • dpowhyh70416 2016-10-31 17:13
    关注

    You need to keep cookies on your side:

    $casper->setOptions(array(
        'ignore-ssl-errors' => 'yes',
        'cookies-file' => 'cookies.txt'
    ));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口