I am trying to implement a new feature: the user of a PHP based site clicks on a link and is logged in into Perl based site. The logging in process itself works flawlessly. However, once I am in the Perl based site, once I click on any link, it asks me to log in again.

Upon further investigation. The code doesn't set cookies.

Code:
    $username="john_smith";
    $password="tOpsecret";
    $url="https://somesite.com/login.html";
    $cookiefile='/tmp/MasonX-Request-WithApacheSession-cookie';

    $postdata = "mysite_username=".$username."&mysite_password=".$password;

    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURL_COOKIEFILE, '');//dl
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt ($ch, CURLOPT_REFERER, "https://whatsdown.nyi.net/news/");
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt ($ch, CURLOPT_POST, 1);
    $result = curl_exec ($ch);
    $result = preg_replace("/<head>/i", "<head><base href='https://somesite.com/' />", $result, 1);
    echo $result;
Interestingly, the cookie does appear in the /tmp directory, but my browser says no cookies were set by the site. What is wrong, could someone help?