How to pass PHP_session_id in curl request
<p>I am learning PHP and trying to automate a website login and then post some data to another page once logged in. I managed to login to the website and when I try to post data, I got a "Document moved". </p>

<p>Then I analysed the headers in firebug and realised that there was a PHP_session_id, so when I tried to manually pass this PHP_session_id it worked. </p>

<p>So my question is, how can I automatically get the sessionid when I login and then subsequently pass this on to my second request?</p>

<p>This is what my code looks like:</p>

<pre><code>&lt;?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"http://www.example.com/login-main.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                "loginType=company&amp;username=johndoe%40gmail.com&amp;password=1234&amp;company=test");

    ob_start();
    curl_exec ($ch);
    ob_end_clean();
    curl_close ($ch);
    unset($ch);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(

    'Host:www.example.com',
    'Origin:http://www.example.com',
    'Cookie:PHPSESSID=na6ivnsdfab206ktb453o2au07',
    'Referer:http://www.example.com/bookings/'

        ));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL,"http://www.example.com/bookings.php");
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                "start_date=&amp;end_date=&amp;type=instructor&amp;b_type=&amp;id=41");
    $buf2 = curl_exec ($ch);
    curl_close ($ch);
    echo "&lt;PRE&gt;".htmlentities($buf2);
?&gt;
</code></pre>