I have a PHP script to be used as an Ajax responder, which I want to remember a previous call. I thought that it should be able to remember using $_SESSION data.
I’ve simplified it down to a pair of scripts, using CURL to communicate between them.
Here is the receiving end code which is supposed to remember:
// test.php
// Start Session
if(!session_id()) session_start();
session_regenerate_id();
// init
if(isset($_GET['init'])) {
$init = uniqid();
$_SESSION['init'] = $init;
# session_write_close(); // doesn’t seem to help
exit($init);
}
// test
if(isset($_GET['test'])) {
exit(print_r($_SESSION, true));
}
Here is the code which calls the above:
// Initialise
$url = 'test.php?init';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
// Test
$url = 'test.php?test';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
I was hoping that the second (test) would include the new session data from the first (init).
I’ve also tried including this when calling:
curl_setopt($curl, CURLOPT_COOKIE, session_name() . '=' . session_id());
session_write_close();
That doesn’t work either.
Maybe sessions aren’t the solution. How can I get the called code to remember between calls?
CURLOPT_COOKIEis for setting your own cookies, which is another way to share data between requests, but that's a different thing. Lastly, if you've found a new solution to how curl can persist a session, add it as a new answer to the appropriate linked dup.I don’t want to set cookies, I want to use PHP sessions.- Alas, "PHP sessions" (or any other sessions for that matter) are based on cookies and the only reason a PHP session wouldn't work is a failure to provide a valid session cookie. So you have to set cookies, like it or not.