开发者

PHP Curl 302 authentication with cookies

I am trying to learn to use PHP curl and it seemed to go well until I have tried to authenticate to changeip.com. Here is the function I use to make a Curl call:

function request($ch, $url, $params = array())
{   
    $options            = array
    (
        CURLOPT_URL                 => $url,
        CURLOPT_USERAGENT           => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8',
        //CURLOPT_COOKIESESSION     => TRUE,
        CURLOPT_FOLLOWLOCATION      => TRUE,
        CURLOPT_HEADER              => TRUE,
        CURLOPT_RETURNTRANSFER      => TRUE,
        CURLOPT_SSL_VERIFYPEER      => FALSE,
        CURLOPT_SSL_VERIFYPEER      => FALSE,
        CURLINFO_HEADER_OUT         => TRUE,
        CURLOPT_CONNECTTIMEOUT      => 30,
        CURLOPT_TIMEOUT             => 30,
        CURLOPT_MAXREDIRS           => 30,
        CURLOPT_VERBOSE             => TRUE,
        CURLOPT_COOKIEJAR           => __DIR__ . DIRECTORY_SEPARATOR . 'cookies.txt',
        CURLOPT_COOKIEFILE          => __DIR__ . DIRECTORY_SEPARATOR . 'cookies.txt',
        CURLOPT_HTTPHEADER          => array
        (
            'Host: www.changeip.com',
            'Pragma:',
            'Expect:',
            'Keep-alive: 115',
            'Connection: keep-alive',
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language: en-us,en;q=0.5',
            //'Accept-Encoding: gzip,deflate',
            'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
            'Content-Type: application/x-www-form-urlencoded',
        ),
    );

    if(!empty($params['referrer']))
    {
        $options[CURLOPT_REFERER]       = $params['referrer'];
    }

    if(!empty($params['post']))
    {
        $options[CURLOPT_POST]          = TRUE;
        $options[CURLOPT_POSTFIELDS]    = $params['post'];
    }

    curl_setopt_array($ch, $options);

    $return             = array();  
    $return['body']     = curl_exec($ch);
    $info               = curl_getinfo($ch);
    //die(var_dump( curl_getinfo($ch, CURLINFO_HEADER_OUT) ));
    $return['header']   = http_parse_headers(substr($return['body'], 0, $info['header_size']));
    $return['body']     = substr($return['body'], $info['header_size']);

    /*if(!empty($return['header']['Location']))
    {
        $params['referrer'] = $url;
        return request($ch, substr($url, 0, strrpos($url, '/')+1) . $return['header']['Location'], $params);
    }*/

    return $return;
}

And here is the actual call:

// chaneip
$ch                 = curl_init();

// login
$params             = array();
$params['post']     = array
(
    'p' => 'aaaaaa2',
    'u' => 'aaaaaa2',
);

$para开发者_运维技巧ms['referrer'] = 'https://www.changeip.com/login.asp';

$return             = request($ch, 'https://www.changeip.com/loginverify.asp?', $params);

However, this script does not retrieve valid cookies from changeip.com, i.e., does not authenticate. I have tried to compare Curl sent headers with HTTPLiveHeaders expecting to find any difference but in the end I didn't find anything. Can anyone advice me what is missing to make this work?

Commonly given question: is cookie.txt 0777? Yes and the script does actually create some sort of cookie: www.changeip.com FALSE / FALSE 0 ACloginAddrs 6 www.changeip.com FALSE / FALSE 0 ASPSESSIONIDCCSSCQRA DNHKGDICMKHFIJADMAPPMHHC

But it isn't a valid cookie.


$options[CURLOPT_POSTFIELDS]    = http_build_query($params['post']);

Fixed the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜