简体   繁体   中英

Cookie issue in CURL (PHP) - Cookie information not attached in CURL headers

I have this code

<?php
    $mLoginUrl = "https://www.test.com/login";
    $mCookieFile = dirname(__FILE__).'/tmpCookies/cookie'.rand().'.txt';

    define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
    define('COOKIE_FILE', $mCookieFile);
    define('LOGIN_FORM_URL', $mLoginUrl);
    define('LOGIN_ACTION_URL', $mLoginUrl);

    $postValues = array(
        'user[email]' => "mymail@email.com",
        'user[password]' => "mypassword"
    );

    $headers = Array(
                "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                "Cache-Control: max-age=0",
                "Connection: keep-alive",
                "Keep-Alive: 300",
                "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                "Accept-Language: en-us,en;q=0.5",
                "Pragma: "
            );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
    curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
    curl_setopt($curl, CURLOPT_COOKIEFILE, COOKIE_FILE);
    curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_HTTPHEADER,$headers) ;
    curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
    $res = curl_exec($curl);
    $info = curl_getinfo($curl);
    print_r($info['request_header']);
    exit;
?> 

This works fine on my local computer and one of my servers and shows following output

GET / HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36
Host: www.test.com
Referer: https://www.test.com/login
Cookie: _property_session=WjROWEYvTHNYaE5Zb29jVk04WGM0Z3FybmhmY1ZIeVdBc2N6d2N3UmViaXlZdFNhR1dSbUN4QVh6aFFSRjFPYktybmdnRGlXNG0yWWcremEzcklKTnE1ZE1lTTM0eUQrSG90SVhRRzhvYW5rWmFQTVhBMjVCWjBtb1FSc0RrTEh2RjhHSFI3aHkwa3U4N3Y3czJhTzJuN2ZGbWRRN0Nra2Z6OTR4aHhvVG42bVVRS3kwTExUL1hMN2JoZ0xRd2g3VVdIMC81cGhLQzJjOTJvc2RYajIwakE0VjZqRnhTeHBleFltTGF4Z3hpUGJEb0E3Nlo2S3BwMElqNnVkaWhDVS0tc0pCRlozSVE5bXRHQXlHWE1IbTl4UT09--67cf6e056b84b4cae4d275507f544927802eb78d
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cache-Control: max-age=0
Connection: keep-aliveKeep-Alive: 300 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Language: en-us,en;q=0.5 

which means that cookie was created and attached in headers of CURL (as we can see in above header print of CURL.) The cookie file is created as well at mentioned location.

But on one of my server the code do not work as per expectations and gives following output

POST /users/sign_in? HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36 Host: www.test.com Referer: https://www.test.com/login Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Cache-Control: max-age=0 Connection: keep-alive Keep-Alive: 300 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Language: en-us,en;q=0.5 Content-Length: 76 Content-Type: application/x-www-form-urlencoded 

Means cookie information is not attached in headers of CURL. I have checked that cookie file is created in this (problematic) server too and is having cookie contents but still cookie contents are not included in CURL headers. Temporary cookie directory mentioned is having full rights/permissions (777) for all users.

PHP version is 5.4.19 and CURL version is 7.19.7 on problematic server.

If anybody can help? I have tried all of the solutions found on internet.

Thanks in advance.

$mCookieFile = dirname(__FILE__).'/tmpCookies/cookie'.rand().'.txt';

删除rand()...成为静态文件

Thanks all for your help. I have solved issue. The issue was open_basedir path. I set this to "none" on server which fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM