Is there such a thing in PHP? Otherwise, I try static fields, $ _SERVER, $ GLOBAL, everything leads to the same result, when the page is reloaded, it all disappears.

That is, such code (or similar with other methods):

$_SERVER['test'] = 'test'; 

When you try to display on another page like this:

 var_dump($_SERVER['test']); 

Gives NULL. Where am I doing something wrong or is there no global object in PHP?

Ps No need to tell that global objects are bad, that if the server crashes, all data will fly off. I just need to pass the lab to the university and forget php forever, so I don’t want to bother with object serialization and saving))

  • one
    “That global objects are bad” - no one told you that. In PHP, this concept is simply absent. Use some kind of storage. Memcache or something like that. - Ruslan
  • at the application level - any options for caching, at the client level - session. If you have a lab in the uni then this session. - teran

2 answers 2

In the first line write session_start()
Further, even when the page is reloaded, the $ _SESSION array will not be reset.
The contents of the array will be available only to the user who launched the script (opened the page).
To access shared data by multiple users, you must use a database or cache. Depends on the task.

 $array = [1,2,34,4,5]; file_put_contents('file.txt', serialize($array)); Получение данных: $array = unserialize(file_get_contents('file.txt')); 
  • Oh, that seems to be what you need, thank you) - Uraty

This can be achieved by working with sessions, for example. $_SESSION[someVar] = 'haxfax'; However, before this session must be initialized.

Read more here: $ _SESSION


Either write to file or in db

With the file in my opinion, the most crutch-simple way.

  • Alas, this is completely contrary to the main goal, because of which I decided to resort to such evil as a global object - to avoid serialization and saving to the database - Uraty
  • @Uraty well then session / file. In fact, these are quite similar things, considering that the pkhp session writes to files, only there is more functional. - SLy_huh