title: PHP Curl And Cookies
body: <p>I have some problem with PHP Curl and cookies authentication.</p>
  
  <p>I have a file <strong>Connector.php</strong> which authenticates users on another server and returns the cookie of the current user.</p>
  
  <p>The Problem is that I want to authenticate thousands of users with curl but it authenticates and saves COOKIES only for one user at a time.</p>
  
  <p>The code for connector.php is this:</p>
  
  <pre><code>    &lt;?php
      if(!count($_REQUEST)) {
          die("No Access!");
      }
  
  
      //Core Url For Services
      define ('ServiceCore', 'http://example.com/core/');
  
  
      //Which Internal Service Should Be Called
      $path = $_GET['service'];
  
  
      //Service To Be Queried
      $url = ServiceCore.$path;
  
      //Open the Curl session
      $session = curl_init($url);
  
      // If it's a GET, put the GET data in the body
      if ($_GET['service']) {
          //Iterate Over GET Vars
          $postvars = '';
          foreach($_GET as $key=&gt;$val) {
              if($key!='service') {
                  $postvars.="$key=$val&amp;";
              }
          }
          curl_setopt ($session, CURLOPT_POST, true);
          curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
      }
  
  
      //Create And Save Cookies
      $tmpfname = dirname(__FILE__).'/cookie.txt';
      curl_setopt($session, CURLOPT_COOKIEJAR, $tmpfname);
      curl_setopt($session, CURLOPT_COOKIEFILE, $tmpfname);
  
      curl_setopt($session, CURLOPT_HEADER, false);
      curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
  
      // EXECUTE
      $json = curl_exec($session);
          echo $json;
      curl_close($session);
  ?&gt;
  </code></pre>
  
  <p>Here is the process of authentication:</p>
  
  <ol>
  <li>User enters username and password: <code>Connector.php?service=logon&amp;user_name=user32&amp;user_pass=123</code></li>
  <li><code>Connector.php?service=logosessionInfo</code> returns info about the user based on the cookies saved earlier with logon service.</li>
  </ol>
  
  <p>The problem is that this code saves the cookie in one file for each user and can't handle multiple user authentications.</p>