2017-02-24 1 views
0

이 질문은 여러 번 전에 Stack-Overflow에서 묻지 만 답변으로는 내 문제를 해결하지 못했습니다.컬 PHP로 세션을 유지하는 방법?

내가 원격으로 JSP 사이트를 찾아 컬 사용하는 PHP 스크립트 쓰고 있어요 : 나는 쿠키 파일이 생성 된 코드를 실행하지만 난 오류가 발생하는 경우

<?php 
$loginUrl = 'http://ccc.hyundai-motor.com/servlet/ccc.login.CccLoginServlet'; 

$sh = curl_share_init(); 
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SHARE, $sh); 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,'the post data including my username and password and other hidden fields'); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_exec($ch); 

$ch2 = curl_init(); 
curl_setopt($ch2, CURLOPT_SHARE, $sh); 
curl_setopt($ch2, CURLOPT_URL,'http://ccc.hyundai-motor.com/servlet/ccc/admin/user/UserInfoServlet?cmd=myUserInfo'); 
curl_setopt($ch2, CURLOPT_COOKIEFILE,'cookie.txt'); 
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch2, CURLOPT_VERBOSE, true); 
$result = curl_exec($ch2); 
print $result; 
curl_share_close($sh); 
curl_close($ch); 
curl_close($ch2); 
?> 

"세션이 손실 : 여기에 나의 코드 , 다시 로그인하십시오. "

+0

http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php#13020494 – JustOnUnderMillions

+0

전에는 문제가 아니지만 새로운 것은 없습니까? 내 코드의 문제에 대해 더 구체적으로 설명 할 수 있습니까? –

+0

첫 번째 호출에서는'CURLOPT_COOKIEJAR | CURLOPT_COOKIEFILE'과'CURLOPT_COOKIEJI' 두 번째 부분에서'CURLOPT_COOKIEFILE' 중 하나만 사용해야합니다. http://stackoverflow.com/a/13020460/4916265 – JustOnUnderMillions

답변

0

curl_share_init을 통해 핸들을 만들어 각 cURL 인스턴스에 전달해야합니다. 그렇지 않으면 인스턴스가 별도로 존재하며 필수 세션 쿠키를 공유 할 수 없습니다.

PHP 매뉴얼에서 예 : 이미 읽었습니다

// Create cURL share handle and set it to share cookie data 
$sh = curl_share_init(); 
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); 

// Initialize the first cURL handle and assign the share handle to it 
$ch1 = curl_init("http://example.com/"); 
curl_setopt($ch1, CURLOPT_SHARE, $sh); 

// Execute the first cURL handle 
curl_exec($ch1); 

// Initialize the second cURL handle and assign the share handle to it 
$ch2 = curl_init("http://php.net/"); 
curl_setopt($ch2, CURLOPT_SHARE, $sh); 

// Execute the second cURL handle 
// all cookies from $ch1 handle are shared with $ch2 handle 
curl_exec($ch2); 

// Close the cURL share handle 
curl_share_close($sh); 

// Close the cURL handles 
curl_close($ch1); 
curl_close($ch2); 
+0

내가 말한대로 시도했지만 그것은 여전히 ​​같은 오류를주고, 나는 그에 따라 질문에 코드를 업데이 트했습니다, 제발 좀 봐. –

관련 문제