3

I have a php script that will make a series of requests on a server. The first request will be a login request.

The problem is that file_get_contents seems to create a new session every time, so how can I make it session aware?

Here is my function which needed to make it remember session:

function request($method, $data, $url){
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n"."Cookie: ".session_name()."=".session_id()."\r\n",
            'method'  => $method,
            'content' => http_build_query($data),
        ),
    );

    $context  = stream_context_create($options);
    session_write_close();
    $result = file_get_contents($url, false, $context);

    return $result;
}
Florian Lemaitre
  • 5,905
  • 2
  • 21
  • 44
Hazem Hagrass
  • 9,329
  • 10
  • 32
  • 54
  • 2
    Should be obvious: By passing the existing session id … // This would probably be easier if you used cURL instead, because that has means for proper cookie handling across requests on board already. – CBroe Feb 17 '16 at 14:13
  • 1
    With lots of effort. Keep and extract the `$http_response_header` to repopulate the request [context](http://stackoverflow.com/questions/2107759/php-file-get-contents-and-headers) with the according session cookie. – mario Feb 17 '16 at 14:14
  • @mario but this is an html pages not an api – Hazem Hagrass Feb 17 '16 at 14:15
  • @CBroe please check my code above – Hazem Hagrass Feb 17 '16 at 14:16
  • 3
    And how is that relevant? Why does your code assume the local session_id() and name to be identical to the remote one? – mario Feb 17 '16 at 14:17
  • As @mario said, you of course need to use the session name and id the remote server returns, not your local ones. And to get those, you need to extract them from the response headers of your first request, that starts the session on the remote server. Again: _Much_ easier with cURL. – CBroe Feb 17 '16 at 14:25
  • @CBroe can you post how can I do it with curl? – Hazem Hagrass Feb 17 '16 at 14:26
  • 1
    No, please go research that yourself. – CBroe Feb 17 '16 at 14:27

2 Answers2

1

I just used curls instead of file_get_contents and everything works well with me:

function request_url($method='get', $vars='', $url) {
    $ch = curl_init();
    if ($method == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
    $buffer = curl_exec($ch);
    curl_close($ch);
    return $buffer;
}
Dan
  • 59,490
  • 13
  • 101
  • 110
Hazem Hagrass
  • 9,329
  • 10
  • 32
  • 54
1

Explanation of why the cookies work in cURL in Hazem's answer.

Author: Nate I

The magic happens with CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE.

CURLOPT_COOKIEJAR tells cURL to write all internally known Cookies to the specified file. In this case, 'cookies/cookies.txt'. For most users, it's generally recommended to use something like tempnam('/tmp', 'CURLCOOKIES') so you know for sure that you can generate this file.

CURLOPT_COOKIEFILE tells cURL which file to use for Cookies during the cURL request. That's why we set it to the same file we just dumped the Cookies to.

Community
  • 1
  • 1
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51