Saturday, July 17, 2010

curl + cookie + redirect = nightmare

Sometime we use curl to store cookies in the remote server. and do our workings accordingly. Curl comes up with two options to deal with cookie settings remotely. They are the followings:

  • COOKIEJAR: it sets the cookie variables in the local machine and stores it in a file. we store it for later use. This file is written in netscap cookie file format. the values are delimited with a tab. A cookie is stored on a single line.
  • COOKIEFILE: This variable sends the cookie file to the remote server.

see the following code block. it explains the above discussion.

//create cURL connection
        $curl_connection = curl_init('remote_server_url');

        //set options
        curl_setopt($curl_connection, CURLOPT_POST, TRUE);
        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, false);        
        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);        
        //set data to be posted        
        curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($curl_connection, CURLOPT_COOKIEJAR, 'cookies.txt');
        curl_setopt($curl_connection, CURLOPT_COOKIEFILE, 'cookies.txt');

Here we send the cookie with the last two lines.

Now if we redirect our header in the remote server, then there will be a problem. your server then could not find the cookie file. and you will be signed out or related problem will be arose.

To solve this problem, you need to set your cookie after redirect call is made. So, you need to set cookie like this after a redirection is made.

setcookie('asite', $site, time()+60*60, '/', ''); 

See the book OpenCart 1.4 Template Design Cookbook.
See the book Joomla Mobile Development Beginners Guide

List of my works:

Opencart Extensions:

  1. Product Based Quantity Wise Shipping: Find it here.
  2. OpenSSLCOMMERZ: integrate SSLCOMMERZ with opencart: Find it here.
  3. Fine Search v.1.0 - Improves Opencart search feature to find relevant: Find it here.
  4. Opensweetcaptcha - An easy way to generate attractive captcha for your system!: Find it here.
  5. Custom Field Product - add unlimited custom fields to the product form: Find it here.
  6. Formcaptcha - add captcha on the register page: Find it here.

My Books:

  1. OpenCart 1.4 Template Design Cookbook.
  2. Joomla Mobile Development Beginners Guide

No comments: