PHP Curl And Cookies PHP Curl And Cookies php php

PHP Curl And Cookies


You can specify the cookie file with a curl opt. You could use a unique file for each user.

curl_setopt( $curl_handle, CURLOPT_COOKIESESSION, true );curl_setopt( $curl_handle, CURLOPT_COOKIEJAR, uniquefilename );curl_setopt( $curl_handle, CURLOPT_COOKIEFILE, uniquefilename );

The best way to handle it would be to stick your request logic into a curl function and just pass the unique file name in as a parameter.

    function fetch( $url, $z=null ) {            $ch =  curl_init();            $useragent = isset($z['useragent']) ? $z['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';            curl_setopt( $ch, CURLOPT_URL, $url );            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );            curl_setopt( $ch, CURLOPT_AUTOREFERER, true );            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );            curl_setopt( $ch, CURLOPT_POST, isset($z['post']) );            if( isset($z['post']) )         curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] );            if( isset($z['refer']) )        curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] );            curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );            curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($z['timeout']) ? $z['timeout'] : 5 ) );            curl_setopt( $ch, CURLOPT_COOKIEJAR,  $z['cookiefile'] );            curl_setopt( $ch, CURLOPT_COOKIEFILE, $z['cookiefile'] );            $result = curl_exec( $ch );            curl_close( $ch );            return $result;    }

I use this for quick grabs. It takes the url and an array of options.


In working with a similar problem I created the following function after combining a lot of resources I ran into on the web, and adding my own cookie handling. Hopefully this is useful to someone else.

      function get_web_page( $url, $cookiesIn = '' ){        $options = array(            CURLOPT_RETURNTRANSFER => true,     // return web page            CURLOPT_HEADER         => true,     //return headers in addition to content            CURLOPT_FOLLOWLOCATION => true,     // follow redirects            CURLOPT_ENCODING       => "",       // handle all encodings            CURLOPT_AUTOREFERER    => true,     // set referer on redirect            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect            CURLOPT_TIMEOUT        => 120,      // timeout on response            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects            CURLINFO_HEADER_OUT    => true,            CURLOPT_SSL_VERIFYPEER => true,     // Validate SSL Certificates            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,            CURLOPT_COOKIE         => $cookiesIn        );        $ch      = curl_init( $url );        curl_setopt_array( $ch, $options );        $rough_content = curl_exec( $ch );        $err     = curl_errno( $ch );        $errmsg  = curl_error( $ch );        $header  = curl_getinfo( $ch );        curl_close( $ch );        $header_content = substr($rough_content, 0, $header['header_size']);        $body_content = trim(str_replace($header_content, '', $rough_content));        $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m";         preg_match_all($pattern, $header_content, $matches);         $cookiesOut = implode("; ", $matches['cookie']);        $header['errno']   = $err;        $header['errmsg']  = $errmsg;        $header['headers']  = $header_content;        $header['content'] = $body_content;        $header['cookies'] = $cookiesOut;    return $header;}


First create temporary cookie using tempnam() function:

$ckfile = tempnam ("/tmp", "CURLCOOKIE");

Then execute curl init witch saves the cookie as a temporary file:

$ch = curl_init ("http://uri.com/");curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);$output = curl_exec ($ch);

Or visit a page using the cookie stored in the temporary file:

$ch = curl_init ("http://somedomain.com/cookiepage.php");curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);$output = curl_exec ($ch);

This will initialize the cookie for the page:

curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);