Home  >  Article  >  Backend Development  >  What to do if php session is unstable

What to do if php session is unstable

藏色散人
藏色散人Original
2020-08-24 09:57:072473browse

The solution to the unstable php session: first set the sessionState node in the configuration file; then set the class to a serializable class; and finally start the "asp.net state service".

What to do if php session is unstable

Recommended: "PHP Video Tutorial"

Solution to the problem of session instability

The company's system has always had sessions suddenly lost. I thought of a way yesterday, but it didn't work. I solved it this morning by searching for information.

Yesterday I wanted to create a new data table to store user login information. However, when I want to mark a user, I want to use IP to mark it. I think it is not practical because sometimes the IP address of the external network It's the same, so I wanted to use the MAC address to solve it. You can get the MAC of the specified IP through sendarp, but this only applies to LAN and cannot be implemented in non-LAN, so this idea is declared bankrupt. Unless it is obtained through js on the client and then sent to the server, but considering that it would be troublesome to check every time without client cookies, I gave up.

Today I found on the Internet that the sessionState node can be set in the configuration file:

   <sessionState mode="Off|InProc|StateServer|SQLServer"
              cookieless="true|false"
              timeout="number of minutes"
              stateConnectionString="tcpip=server:port"
              sqlConnectionString="sql connection string"
              stateNetworkTimeout="number of seconds"/>

The required attributes are

属性      选项             描述 
mode                       设置将Session信息存储到哪里 
            Off              设置为不使用Session功能 
            InProc          设置为将Session存储在进程内,就是ASP中的存储方式,这是默认值。 
            StateServer  设置为将Session存储在独立的状态服务中。 
            SQLServer    设置将Session存储在SQL Server中。

The optional attributes are:

属性             选项                             描述 
cookieless                                        设置客户端的Session信息存储到哪里 
                   ture                             使用Cookieless模式 
                   false                             使用Cookie模式,这是默认值。 
                   timeout                        设置经过多少分钟后服务器自动放弃Session信息。默认为20分钟 
                   stateConnectionString    设置将Session信息存储在状态服务中时使用的服务器名称和端口号,例如:"tcpip=127.0.0.1:42424”。当mode的值是StateServer是,这个属性是必需的。 
        sqlConnectionString        设置与SQL Server连接时的连接字符串。例如"data source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"。当mode的值是SQLServer时,这个属性是必需的。 
        stateNetworkTimeout    设置当使用StateServer模式存储Session状态时,经过多少秒空闲后,断开Web服务器与存储状态信息的服务器的TCP/IP连接的。默认值是10秒钟。

Then, start the asp.net state service.

Be careful to set the class as a serializable class! can be added to the session.

      System.Web.SessionState.HttpSessionState session = HttpContext.Current.Session;
      session.Add("user", obj);
      HttpContext.Current.Session.Add("user", obj);
      if (obj.USER_DIST_ID == "0000")
      {
             HttpContext.Current.Session.Add("sDistID", obj.USER_DIST_ID);
       }
      System.Web.Security.FormsAuthentication.SetAuthCookie(obj.USER_ID , false);
     。。。

The above is the detailed content of What to do if php session is unstable. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn