How to set a session after curl request
<p>I am trying to set a session from the response of a curl header. And then execute a new curl with the previous session set. But it seems that I still do not get logged in.</p>

<p>The firl cURL execute looks like this:</p>

<pre><code>$url = "https://www.tyre24.com//nl/nl/user/login/userid/MYUSERID/password/MYPASSWORD/page/L2V4cG9ydC9kb3dubG9hZC90L01nPT0vYy9NVFE9Lw==";


$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$response = curl_exec($ch); 

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

curl_close($ch);
</code></pre>

<p>Then to retrieve the PHPSESSID from the header response I use the following code: </p>

<pre><code>$arrResult = explode("PHPSESSID=", $header);
$arrResult = explode(";", $arrResult[1]);

session_id("$arrResult[0]");
session_start();
</code></pre>

<p>So far is everything going alright, when I execute the code I get the value of the PHPSESSID in the variable. </p>

<p>But when I set the session with the 2 lines on the bottom, it seems like there is noting set or something. Atleast after the session is started I tried to print the all sessions. But it prints an empty array.</p>

<p>So it could be that I did something wrong here.</p>

<p>But there needs to be a second request after the first request, because the url from the first request will link me to a new URL to download a file.</p>

<p>So the second request looks like this:</p>

<pre><code>$second_url = "https://www.tyre24.com/nl/nl/export/download/t/Mg==/c/MTQ=/";

$fp = fopen ('result.zip', 'w+');

$ch = curl_init(str_replace(" ","%20",$second_url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);

curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$response = curl_exec($ch); 

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

curl_close($ch);
fclose($fp);
</code></pre>

<p>Can anyone see what I am doing wrong ?
Or can I do everything in one cURL call ?</p>

<p>I keep getting the message that I am not logged in.</p>