通过PHP登录网站分三步

时间:2015-12-04 05:32:19

标签: php curl login

我可以通过PHP登录网站并获取内容并执行其他操作。看我的编码:

    <?php

    //Upload a blank cookie.txt to the same directory as this file with a CHMOD/Permission to 777
    function login($url,$data){
        $fp = fopen("cookie.txt", "w");
        fclose($fp);
        $login = curl_init();
        curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
        curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($login, CURLOPT_TIMEOUT, 40000);
        curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($login, CURLOPT_URL, $url);
        curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($login, CURLOPT_POST, TRUE);
        curl_setopt($login, CURLOPT_POSTFIELDS, $data);
        ob_start();
        return curl_exec ($login);
        ob_end_clean();
        curl_close ($login);
        unset($login);    
    }                  

    function grab_page($site){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($ch, CURLOPT_URL, $site);
        ob_start();
        return curl_exec ($ch);
        ob_end_clean();
        curl_close ($ch);
    }

    function post_data($site,$data){
        $datapost = curl_init();
            $headers = array("Expect:");
        curl_setopt($datapost, CURLOPT_URL, $site);
            curl_setopt($datapost, CURLOPT_TIMEOUT, 40000);
        curl_setopt($datapost, CURLOPT_HEADER, TRUE);
            curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($datapost, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($datapost, CURLOPT_POST, TRUE);
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
            curl_setopt($datapost, CURLOPT_COOKIEFILE, "cookie.txt");
        ob_start();
        return curl_exec ($datapost);
        ob_end_clean();
        curl_close ($datapost);
        unset($datapost);    
    }
?>
<?php
    //for grabbing:
  echo function grab_page("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");
   ?>
And for login it is:
<?php
    function login(https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier,username and password){
    $fp = fopen("cookie.txt", "w");
?>

这个东西只是抓住页面并自动登录并正常工作,并且可以在大多数网站上运行。 问题是我正在尝试登录的网站是3步,意味着在输入用户名和密码后它给我的屏幕如下: http://i.imgur.com/sL7Pwgn.png勾选图像并输入密码。 即使在输入之后,它也会提供如下新屏幕: http://i.imgur.com/3iLDFFI.png每当我作为另一个登录时随机出现:i.imgur.com/Ee85kre.png 问题是我如何通过PHP curl登录这些障碍。我搜索谷歌,但即使只是登录脚本来了。请提出建议。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用数据库中与用户连接的令牌。每个用户都有一个独特的。

注册时,您可以使用用户名,密码ip和salt创建一个。然后,您使用URL中的令牌将链接发送给新用户,当您的脚本选中该令牌时,请在所有设置正确时将用户登录。