2014-02-21 5 views
0

컬링 HTTP POST 요청을 발행 한 간단한 PHP 스크립트를 사용하여 리디렉션을 통해 사용자에게 데이터를 표시합니다. 내가 가진 문제는 둘 이상의 사람이 동시에 스크립트를 실행하면 한 사람이 성공적으로 실행되고 완료되지만 다른 사람에게는 실패합니다. 세션 또는 쿠키와 관련된 것으로 생각되지만 session_start()를 사용하지 않고 쿠키가 리디렉션되기 전에 지워집니다.PHP 스크립트를 동시에 실행하는 사용자

왜 이런 일이 발생하며 동시 사용자를 지원하도록 스크립트를 조정할 수 있습니까?

<?php 
     $params = "username=" . $username . "&password=" . $password . "&rememberusername=1"; 
     $url = httpPost("http://www.mysite.com/", $params); 
     removeAC(); 
     header(sprintf('Location: %s', $url)); 
     exit; 


     function removeAC() 
     { 
      foreach ($_COOKIE as $name => $value) 
      { 
      setcookie($name, '', 1); 
      } 
     } 

    function httpPost($url, $params) 
    { 
     try { 
      //open connection 
      $ch = curl_init($url); 

      //set the url, number of POST vars, POST data 
      // curl_setopt($ch, CURLOPT_COOKIEJAR, "cookieFileName"); 
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
      curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); 
      curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); 
      curl_setopt($ch, CURLOPT_HEADER, true); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
      curl_setopt($ch, CURLOPT_POST, 1); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

      //execute post 
      $response = curl_exec($ch); 

      //print_r(get_headers($url)); 

      //print_r(get_headers($url, 0)); 

      //close connection 
      curl_close($ch); 
      return $response; 
      if (FALSE === $ch) 
       throw new Exception(curl_error($ch), curl_errno($ch)); 

      // ...process $ch now 
     } 
     catch(Exception $e) { 

      trigger_error(sprintf(
       'Curl failed with error #%d: %s', 
       $e->getCode(), $e->getMessage()), 
       E_USER_ERROR); 

     } 
    } 


?> 
+3

요청마다 고유 한 쿠키 병 파일을 만드시겠습니까? –

+0

'$ url = httpPost ("http://www.mysite.com/, $ params);' –

답변

1

올바르게 이해하고 있다면 액세스하는 사이트가 세션/쿠키를 사용하고 있습니까? 이 문제를 해결하려면 각 요청마다 고유 한 쿠키 항아리를 만드십시오.

// at the beginning of your script or function... (possibly in httpPost()) 
$cookie_jar = tempnam(sys_get_temp_dir()); 

// ... 
// when setting your cURL options: 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); 

// at the end of your script or function (when you don't need to make any more requests using that session): 
unlink($cookie_jar); 
+0

당신은 내 친구에게 상을 줄 자격이 있습니다. , 그래서 그것은 Windows 및 Linux 테스트를 기반으로 작동합니다. 그 외에도 매력처럼 작동합니다. 정말 고마워요! @ TajMorton – snapplex