PHP session is reset when switching tabs in one domain

I have a website (www.mysite.com) with a private backend (www.mysite.com/admin)

When I add content to the site in the admin area and switch between tabs in the same browser window to see the content that I am editing, my session ends / ends / ends and I am redirected to the login page again.

I have used the same code many times on many websites (this is a CMS that I made myself) without problems. The only thing I can think of is that this website is hosted on a different web server, and maybe this is a matter of setting up php.ini or server configuration. Any ideas?

+4
source share
7 answers

A few things to check:

  • session.cookie_lifetime setting - perhaps too short; 0 is the default and keeps the cookie until the browser closes
  • session.cookie_path - you want this to be '/'
  • Session Storage - Verify that session data is written.
  • Explicitly calling session_close() if your sessions are stored in a database. This ensures that they will be recorded before the destruction of your database objects and resources.
  • If you are using any proxy server, check for the changed header information.
  • If you use caching, check that your dynamic pages (requiring sessions) are served by your web application and not by the cache.
  • If you are testing local / etc / hosts, first clear the cookies so that the new cookies are fresh and do not conflict.
  • Confirm in your browser that the cookie is actually being stored. Perhaps, in fact, it does not return to the title.
+1
source

Have you checked your browser cookies? (actual client parties?) or tried your luck with a different browser? This may seem a little strange, but I had a similar problem, and in my case it is related to these cookies. It may be helpful to find out due to your strange problem. As you know, the phpsession value is stored in this cookie as well as the domain. Good luck

+1
source

This may be the result of several things, but my first instinct is to check and verify whether session cookies end very quickly. Sometimes server headers can change expiration values. You can also check the cache headers sent by the server. If you use asynchronous functions in the administration area, it is possible that somehow the server changes the expiration date of the cached files, which can affect this.

I really want to see a solution to this issue.

+1
source

I had the same problem as before. I just downloaded the site from my local host to the remote host, and I have not changed the name servers yet. The hosting company provided me with a temporary URL to be able to see my site. The problem was that this URL was https://server_name.grserver.gr:8443/sitepreview/http/my_site.gr/ , as a result, any browser did not accept session cookies because I did not have an SSL certificate, therefore, the sessions did not work at all. I looked at the plesk panel a bit and I found another temporary URL that used the http protocol, and everything was fine. Therefore, if you use https, try to check if you have a problem with your ssl certificate (for exception, if it has expired). You said that the problem occurs when you enter the administration page, then you switch to https?

0
source

There may be several reasons. Since there is no code or no information about the site provided, I assume that the problem may occur if you use htpasswd . If u uses htaccess authentication, your session will be destroyed.

0
source

From experience, I can tell you a few things.

First you need to start sessions with

 session_start(); 

At the top of each page you want to use sessions.

Then, to save the session data, you need to call another function to tell php that you are saving the saved data. This function

 Session_write_close(); 

This feature is needed at the bottom of the page when you are finished writing data to the session and want to save it for future use.

With these two combinations, which should allow you to correctly record on the session, save the data that you entered into it, and access it later on your website.

Good luck.

0
source

The problem was found after reading this section .

I had my own php.ini in the root directory and apparently it interfered with $ _SESSION. I don’t know why, but after removal everything works fine.

At first it seemed that the problem was opening pages located in different subfolders on several browser tabs, but this narrowed down to the problem with the subfolders and the fact that $ _SESSION was not accessible through them.

I would like to thank everyone who took some time to help me figure this out.

0
source

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


All Articles