简体   繁体   中英

php cookies on load balanced web servers

I have setup a simple user login session as below in the pages of my web app:

if (!isset($_SESSION['username'])){
            if (isset($_COOKIE['username'])){   
                $_SESSION['username'] = $_COOKIE['username'];
            }

I started to notice that on some occasions I would loose my login session. I checked the cookie expiry time and that was definitely set for a future date. The behaviour was quite random where sometimes replicating the action would not cause the issue.

Today I discovered that the web servers (x2) are load balanced (clumsy of me to not have picked up on this) and now I suspect the issue spits up when a user sends a request to the 2nd web server where a cookie doesn't exist.

I would have thought when you hit a web server it would maintain a session with it. However the behaviour suggests otherwise.

I have not spoken to the web admin yet. Is there a magic solution the web admin can sort me out with? or is this an implementation problem? If so, any ideas on how I can solve this?

Suggestions are much appreciated.

The cookie doesnt care what backend server handles the request, unless the url changes. If the url changes from www1.xxx.xx to www2.xxx.xx then you could save the cookies with the path included (xxx.xx) and both subdomains will be able to see the cookie.

Another thing that is more likely to go wrong is that the sessions arent shared between both servers. You could use memcached for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM