Monday, 11 February 2019

How to pass session values from one request to another request consecutively using cURL in PHP?

When a first request is made, a session id is created. The same session id is being carried to the second request.

In my third request, a new session ID is created for successful login but when I'm printing the session ID for the third request the response is giving a different session id. Why is this happening? And I want to send the session id that I got in the 3rd response to the 4th?

How to achieve this?

enter image description here

This is my code:

<?php
$fp = fopen("cookies.txt", "w");
fclose($fp);
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_URL => 'https://192.168.2.35/cgi-bin/common/login/webLogin',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_COOKIESESSION => TRUE,
    CURLOPT_COOKIEFILE => "cookies.txt",
    CURLOPT_COOKIEJAR => "cookies.txt",
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_AUTOREFERER => TRUE,
));
$result = curl_exec($curl);

if (!curl_exec($curl))
{
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 1</h2>";
print_r($result);
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);

// #######################################################################################

$fields = array(
    'userName' => 'dadmin',
    'logonButton' => 'Logon',
    'actionStep' => 2,
);
$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_URL => 'https://192.168.2.35/cgi-bin/common/login/webLogin',
    CURLOPT_POST => TRUE,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_POSTFIELDS => $fields_string,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_COOKIESESSION => TRUE,
    CURLOPT_AUTOREFERER => TRUE,
));
$resp = curl_exec($curl);

if (!curl_exec($curl))
{
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 2</h2>";
print_r($resp);
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);

// ################################################################################################

$fields = array(
    'userName' => urlencode('dadmin') ,
    'pa55word' => urlencode('dadmin01') ,
    'logonButton' => urlencode('Logon') ,
    'actionStep' => urlencode(3) ,
);
$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_URL => 'https://192.168.2.35/cgi-bin/common/login/webLogin',
    CURLOPT_POST => TRUE,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_POSTFIELDS => $fields_string,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_COOKIESESSION => TRUE,
    CURLOPT_AUTOREFERER => TRUE,
));
$response = curl_exec($curl);

if (!curl_exec($curl))
{
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 3</h2>";
print_r($response);
echo "<br/>";
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);

// ###########Login Completed##################

curl_setopt_array($curl, array(
    CURLOPT_COOKIESESSION => TRUE,
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_URL => 'https://192.168.2.35/cgi-bin/msg/mango/admin/controller/SubscriberMgmt',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_AUTOREFERER => TRUE,
));
$result = curl_exec($curl);

if (!curl_exec($curl))
{
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 4</h2>";
print_r($result);
echo "<br/>";
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);
exit;
?>



from How to pass session values from one request to another request consecutively using cURL in PHP?

No comments:

Post a Comment