PHP does not store data in sessions for some users

The internal server serves two different applications for users on our local network:

https://10.0.0.100/app1/ https://10.0.0.100/app2/ 

Both applications are served by the same Apache configuration file, they are only in separate directories under public_html . Both applications use PHP sessions, but both of these $_SESSION prefix variables have a namespace:

 $_SESSION['app1_favorite_colour'] = 'Yellow'; $_SESSION['app2_quest'] = 'Agghhh!'; 

Despite this, users who logged into app1 do not have their app2_ session data written to the session file on disk . Consider page1.php :

 <?php /** * App2 * /page1.php */ session_start(); $_SESSION['apps_been_here'] = 'Yes!'; echo "<a href='page2.php'>Clicky</a>"; ?> 

Then on page2.php :

 <?php /** * App2 * /page2.php */ session_start(); echo "Have you been here: {$_SESSION['apps_been_here']}"; 

This means that it does not print β€œyes” to the user if the user also logs into application 1! However, users who do not use app1 do look yes at page2.php . While researching, I opened the session files in /var/lib/php/session/ . I see only the variables in this file that start with the app1_ prefix and not one that starts with app2_ for users who are logged in to application1. Other users who are not logged in to app 1 have variables that start with app2_ in the session files!

I checked with lsof that the files were not locked, and I confirmed that the browser does not have open browser windows with app1 pages. Why can't App2 session variables be stored in a session file? There is a lot of free space on the disk and on the hard disk, and the processor load is less than 0.1 (measured using uptime ). The problem occurs for multiple users in multiple browsers (Firefox, Chrome). Clearing browser cookies and cache cookies does not help.

+6
source share
3 answers

I believe that what you do should work, but there is a better way to do this to solve your problem in your case. Instead of prefixing your session keys with "app1" or "app2" just use the session_name () function before session_start (), for example:

 session_name('app1'); session_start(); $_SESSION['been_here'] = 'Yes!'; ... 

You can also configure the session cookie path using session_set_cookie_params () so that both applications are completely invisible to each other, for example: session_set_cookie_params (60 * 60 * 24 * 30, '/ app1 /');

For further troubleshooting, you should investigate wherever you use the session_ * functions, and look at session. * configuration parameters in php.ini.

+1
source

try it

 session_start(); $_SESSION['apps_been_here'] = 'Yes!'; echo "<a href='page2.php'>Clicky</a>"; session_write_close(); 

I had such a problem. It fixed it for me!

0
source

Not good, they must be in the same subtree you need to have

  • root /
    • Myapp /
      • session.php
      • app1 /
      • app2 /

session.php will only contain session_start ();

each app1 and app2 will include session.php (require_once ("../session.php");) is the only way I know to get them to work.

0
source

Source: https://habr.com/ru/post/958995/


All Articles