I am trying to do my very first site regression testing. The site backend consists of several PHP scripts. So I've am just calling all PHP files, one after another, using cURL, with various valid and invalid input and check the result. All works fine until I reach the session-based authentication management. I see that with cURL the _SESSION does not work the same way as with normal calls to PHP from the browser (see below). If I understand correctly, this is because the session functionality requires a cookie on the client side, which is missing in case of using cURL the way I do (I kind of hoped it would happen automagically). So how do I make cURL take care of cookies and call php-files as if called by a browser?
In the example below I expect to see "test" but I see "SESSION not set" instead.
Calling file:
<?php
session_start();
$_SESSION["test"] = "test";
echo sendPost('https://rodichi.net/sandbox/php/test_curl_session_called.php', null);
function sendPost($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ($result === false) {
die(curl_error($ch));
}
curl_close($ch);
return $result;
}
Called file:
<?php
session_start();
if (isset($_SESSION['test'])) {
echo($_SESSION['test'].'<br>');
} else {
echo 'SESSION not set<br>';
}