I wonder why something like this wouldn't work (actually doesn't work):
4.php :
<php
session_start();
$my_id = session_id();
$_SESSION['username'] = (($_SESSION['username'] == 'nimic') ? 's-a schimbat' : $_GET['test']);
file_put_contents('comm.g', PHP_EOL.$my_id, FILE_APPEND);
sleep(3); // can be left out
echo $_SESSION['username'];
?>
4.php is a simple page that only sets the "username" component of $_SESSION, then writes the session id to a file, after which it echoes the $_SESSION['username'] value.
Then:
3.php :
<php
while(true)
{
if(file_exists("comm.g"))
{
$c = file("comm.g");
unlink("comm.g");
session_start();
foreach($c as $k => $v)
{
if($v != '')
{
$my_id = session_id();
session_write_close();
session_id($v);
session_start();
file_put_contents("result.cc", $_SESSION['username'].'---'.$v.'END'.PHP_EOL.PHP_EOL, FILE_APPEND);
$_SESSION['username'] = 'somethingelse';
session_id($my_id);
session_start();
}
}
session_write_close();
}
}
?>
3.php is run by a cronjob, every minute, for about 30 seconds (until server kills it). Anyway in result.cc
I find (in those 30 available seconds) the expected result... the value of $_GET['test'].
But it does not modify the value of $_SESSION['username'], I didn't even expect such a behavior actually, I knew that sleep wouldn't be enough, but still, on a refresh shouldn't it be modified?...
I know that my code is awful, it is just a test, if it works it would be improved.
But I don't seem to figure it out. What am I doing wrong? Actually I am doing a lot of things wrong, the system itself should not be used like this, I know that too, but still, right now this is my only option. I have more complicated ideas about this thing, but if this won't work, the others won't work either.
My intention actually is to execute code in separate thread so that I can escape some changes (like registering a wrapper for the file scheme, and having no power to unregister (restore) it when I need to, inside the wrapper class that is) that I made there. Unorthodox method, I know...
So, can you help with any ideas? Thank you.
Even if lead me to an interesting idea @Ben's answer wasn't exact... Actually in cookies is kept only the session id, I replaced the need for cookies by using a file, so that the cronjob knew what session should be modified, I got the correct value for $_SESSION['username'] for any "registred" sessions, but even so, I was unable to modify the damn values, it was like they were marked readonly. Now, reconsidering, I found the idea still on it's feet, being viable, and a solution whatsoever. So my questions still is up... WHY wouldn't it work?
Here's your problem: 3.php is run by a cronjob
.
The session requires cookies, which requires a web browser and browser state. If you run the script as a cron job, there is no session.
A solution to your actual problem that you might want to consider is Runkit Sandbox: http://php.net/manual/en/runkit.sandbox.php