I
I
igoogex2019-07-29 03:35:24
Sessions
igoogex, 2019-07-29 03:35:24

How to pass session variables between independent scripts?

Hello newbie web developer. There was a problem I can not give the mind. It's in the first file:

// Сессия живет 24 часа
ini_set('session.gc_maxlifetime', 86400);

// Свой идентификатор сессии, нет куки
session_id('test');

// Заново или возобнавляем
session_start();

// Цена на рынке меняется динамически
for ($i = 0; $i < 100; $i++) {
     $_SESSION['price'] = $i;
     sleep(1);
}

It's in the second file:
// Свой идентификатор сессии, нет куки
session_id('test');

// Заново или возобнавляем
session_start();

for ($i = 0; $i < 100; $i++) {
     echo $_SESSION['price'];
     sleep(1);
}

Scripts are launched by cron, it was assumed that the second script will always display the current value of the $_SESSION['price'] variable, but it displays the same value that was in the variable at the time of the first iteration of the loop and does not change during the entire loop, although by hand an open session file shows that it is a value that changes . It is possible that I did not understand something in the technology of the session.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel K, 2019-07-29
@igoogex

Good day.
It seems to be puffing the session file either until the script ends, or until session_write_close () is called; And then when you try to simultaneously record xs that will be recorded.
https://secure.php.net/manual/ru/function.session-...
To exchange data between scripts, you can try https://php.net/manual/en/ref.shmop.php
or sockets https://habr.com/ru/post/209864/ or a memcache https://habr.com/ru/post/108274/ or a file on disk and serialization/deserialization from JSON.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question