title: Drupal log in with a curl
body: <p>I'm trying to log in a drupal via curl. I got message: 'logged' with a command:
  echo "logged"; which is in a if statement that tells me how everything was fine. </p>
  
  <p>After running a script I open my homepage and unfortunately I wasn't logged in. </p>
  
  <p>I think that I have a problems with a cookies.        </p>
  
  <pre><code> &lt;?php
      ob_start(); // Initiate the output buffer
      function mymodule_get_csrf_header() {
        $curl_get = curl_init();
        curl_setopt_array($curl_get, array(
          CURLOPT_RETURNTRANSFER =&gt; 1,
          CURLOPT_URL =&gt; 'http://will.sx/services/session/token',
        ));
        $csrf_token = curl_exec($curl_get);
        curl_close($curl_get);
        return 'X-CSRF-Token: ' . $csrf_token;
      }
      $username = 'test';
      $password = 'TEST';
      $request_url = 'http://will.sx/rests/user/login';
      $user_data = array(
        'username' =&gt; $username,
        'password' =&gt; $password,
      );
      $user_data = http_build_query($user_data);
  
      $curl = curl_init($request_url);
      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); // Accept JSON response
      curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
      curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
      curl_setopt($curl, CURLOPT_HEADER, FALSE);  // Ask to not return Header
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
      curl_setopt($curl, CURLOPT_COOKIESESSION, true);
      curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt");
      curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt");
  
      $response = curl_exec($curl);
      $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
      if ($http_code == 200) {
        $logged_user = json_decode($response);
        echo 'logged';
      }
      else {
        $http_message = curl_error($curl);
        die('Unable to connect to Basic CMS Engine! 
                                              Username or password incorrect! 
                                              Please enter valid username and password!');  
      }
      //setcookie(name,value,expire,path,domain,secure)
      setcookie($logged_user-&gt;session_name,$logged_user-&gt;sessid,time() + 10000,'/');
      ob_end_flush(); // Flush the output from the buffer
      ?&gt;
  </code></pre>
  
  <p>Every kind of help is welcome.
  Thanks in advance.</p>