首页 > 解决方案 > curl远程登录问题

问题描述

我想远程登录到我的第二个网站,但我遇到了一些问题,因为操作文件是 /user/login.htm 并且表单位于 /user/index.htm

当我尝试在我的第一个网站上发布帖子时,我收到错误消息,提示用户名和密码无效,但我检查了几次,一切正常……帖子是这样的:

continueTo=%2Fuser%2Findex.htm&login=&changeLogInPermanentlyPref=true&email=email%40email.com&password=TESTPASS&submit=&logInPermanently=on&_sourcePage=htYSMjoK0IATah86FF3yMT1zDYzaGsbkGTP5awTC0hykcXEpm9ztqjXVz-aUblAsXV9ykLXP1mm4yo_0TuIk1cjwytYy9KBr&__fp=NhVgRkYa2DMUOPQzpwoBNVSbbb9ZJWU7T8w3OeZ9n2BhRFuQKOPeOQ5rYfieNa75&__at=1584900220._Ai-iNOACLcgaLBOUPwPa551GFQRuWOoDqvLmWby2bU.AXG1Vdh-Vj4V25wzYgcpk3G_HvAZ

脚本是这样的:

    <?php
session_start();
error_reporting(-1);
include "config.php";
    $browser = $_SERVER['HTTP_USER_AGENT'];
    $ip = getenv("REMOTE_ADDR");
    define('BASE_URL', 'https://www.example.com');




    $curl = curl_init();



    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //set user agent
    curl_setopt($curl, CURLOPT_URL, BASE_URL . $_SERVER['REQUEST_URI']);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_HEADER, false); //we need headers so we can see where the redirect is





    //forward cookies from user to server

    $cookies = array();

    foreach ($_COOKIE as $cookie_name => $cookie_value) {
        array_push($cookies, $cookie_name . '=' . $cookie_value);
    }

    $cookies = implode('; ', $cookies);
    $cookies = 'Cookie: ' . $cookies;
    curl_setopt($curl, CURLOPT_HTTPHEADER, array($cookies));

    //forward request type to server
    if($_SERVER['REQUEST_METHOD'] === "GET")
    {
        curl_setopt($curl, CURLOPT_HTTPGET, true);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
    }
    else if($_SERVER['REQUEST_METHOD'] === "POST")
    {
        $post_data = http_build_query($_POST);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    }

    pre_special_handle($curl);

    $result = curl_exec($curl);
    if($result === false)
    {
        exit('A CURL error occurred: ' . curl_error($curl));
    }

    $response = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    //forward cookies to end user
    preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
    $cookies = array();
    foreach($matches[1] as $item){
        parse_str($item, $cookie);
        $cookies = array_merge($cookies, $cookie);
    }
    foreach ($cookies as $cookie_name => $cookie_value){
        setcookie($cookie_name, $cookie_value);
    }
    post_special_handle($curl, $result, $response);



    if($response == 302 || $response == 301)

    {

        $location = get_location($result);

        if($location === false)

        {

            exit('Received a redirect with no location\n\n' . $result);

        }



        header('Location: ' . $location, true, $response);

        curl_close($curl);

        exit();

    }

    echo cleanup_header($result); //Remove the headers and send page to user

    curl_close($curl);



    ///////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////





    /* Handles special URLs before request */

    function pre_special_handle(&$curl)

    {



        else if(preg_match("/\//", $_SERVER['REQUEST_URI']))

        {



        }

    }



    /* Handles special URLs response */

    function post_special_handle(&$curl, &$result, $response)

    {

        if($_SERVER['REQUEST_URI'] == '/login.html')

        {

            //Do whatever you want after fetching page here...

        }

    }



    //////////////////////////////////////////////////////////////////////////



    /* Function to find the redirect location */

    function get_location($header)

    {

        preg_match("/\b[Ll]ocation: ([A-Za-z0-9:\/\._\?=-]+)/", $header, $location_matches);



        if(count($location_matches) <= 1)

        {

            return false;

        }



        //die($header);



        if(strpos($location_matches[1], "http") === false)

        {

            if(substr($location_matches[1], 0, 1) !== '/') //first char is not /, add it

            {

                return '/' . $location_matches[1];

            }

            else

            {

                return $location_matches[1]; //correct format already, /page.html

            }

        }



        $location_matches[1] = substr($location_matches[1], strlen('https://'));

        $first_slash = strpos($location_matches[1], "/");

        if($first_slash !== false)

        {

            return substr($location_matches[1], $first_slash);

        }



        return false;

    }



    function cleanup_header($page)

    {

        $html_start = strpos($page, '<!DOCTYPE');

        if($html_start === false)

        {

            return $page;

        }



        return substr($page, $html_start);

    }

?>

标签: phpcurlremote-login

解决方案


推荐阅读