How to transfer a variable from one PHP script to another, suppose I have a certain number of people displayed on a certain page, I need something to see the number of people on the main page, how to implement it?

  • Through the database?) - Dmitriy Simushev
  • or through sessions - Ilya Shashilov

1 answer 1

You just need to store this variable somewhere:

The first script writes to the database / file.
The second script reads from the database / file.

Something like this (example is purely for understanding):

# вставьте это в конец вашего скрипта $fileWrite = fopen($_SERVER['DOCUMENT_ROOT'] . '/data.txt', 'w'); if(is_number($total_reg)) { fwrite($fileWrite, $total_reg); } fclose($fileWrite); # а это на главную, куда-нибудь повыше $fileRead = $_SERVER['DOCUMENT_ROOT'] . '/data.txt'; if(is_number(file_get_contents($fileRead))) { $needValue = file_get_contents($fileRead); # Затем выводите $needValue где вам нужно } 
  • I understand this script reads another script entirely? - Tvarinskyy
  • Since my main script prescribes participation in the contest on the site, and at the same time shows the number of registered and has a lot of other conditions that I do not need on the home page of the site, I just need to show how many people participate - Tvarinskyy
  • @Tvarinskyy, no, the first script (users-on-page.php) writes the $ var variable to the data.txt file; The second script (admin-page.php) reads the data.txt file and displays the contents of this file. (in this case, the number 22 is stored in this file) - Nikolay
  • For me it is a little difficult, in the php script this is indicated: $ data ['total_reg'] = $ total_reg; And in the file of the tpl format, this: <? Php echo $ total_reg;?> - In theory, can I put the same variable on the main page of the site, will it show the number of registered people? - Tvarinskyy
  • Corrected the answer, try. Do not forget to create an empty data.txt file in the root of your web server :) - Nikolay