0

I know that this question have been asked many times before in Stack-overflow but none of the answers solved my problem.

I am writing a php script that uses curl to remotely browse JSP site: here is my code:

<?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);
?>

When I execute the code the cookie file is created but i get an error "session lost, please re-login".

J'aime Java
  • 46
  • 1
  • 7
  • http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php#13020494 – JustOnUnderMillions Feb 24 '17 at 12:04
  • I have already read that question before but nothing new, can you be more specific about the problem of my code? – J'aime Java Feb 24 '17 at 12:14
  • Thing you should you only one of these `CURLOPT_COOKIEJAR|CURLOPT_COOKIEFILE` `CURLOPT_COOKIEJAR` in the first call and `CURLOPT_COOKIEFILE` in the second part http://stackoverflow.com/a/13020460/4916265 – JustOnUnderMillions Feb 24 '17 at 12:20

1 Answers1

0

You need to create a handle via curl_share_init and pass it to each cURL instance otherwise the instances exist separately and cannot share the required session cookie.

An example from the PHP manual:

// 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);
timclutton
  • 12,682
  • 3
  • 33
  • 43
  • I have tried as you said but it still giving the same error, i have updated the code in the question accordingly, please take a look. – J'aime Java Feb 24 '17 at 12:56