将Session写下Memcache
将Session写入Memcache
通过session_set_save_handler()方法自定义Session写入Memcache
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MemSession{</span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$handler</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$lifetime</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$time</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">const</span> MS = 'session'<span style="color: #000000;">;</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> init(<span style="color: #800080;">$handler</span><span style="color: #000000;">){</span><span style="color: #008080;"> 9</span> self::<span style="color: #800080;">$handler</span> = <span style="color: #800080;">$handler</span><span style="color: #000000;">;</span><span style="color: #008080;">10</span> self::<span style="color: #800080;">$lifetime</span> = <span style="color: #008080;">ini_get</span>('session.gc_maxlifetime'<span style="color: #000000;">);</span><span style="color: #008080;">11</span> self::<span style="color: #800080;">$time</span> = <span style="color: #008080;">time</span><span style="color: #000000;">();</span><span style="color: #008080;">12</span> <span style="color: #000000;"> }</span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> start(<span style="color: #800080;">$memcache</span><span style="color: #000000;">){</span><span style="color: #008080;">15</span> self::init(<span style="color: #800080;">$memcache</span><span style="color: #000000;">);</span><span style="color: #008080;">16</span> <span style="color: #008000;">//</span><span style="color: #008000;">调用类中的方法要用数组,__CLASS__代表本类</span><span style="color: #008080;">17</span> <span style="color: #008080;">session_set_save_handler</span><span style="color: #000000;">(</span><span style="color: #008080;">18</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'open'),<span style="color: #008080;">19</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'close'),<span style="color: #008080;">20</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'read'),<span style="color: #008080;">21</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'write'),<span style="color: #008080;">22</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'destroy'),<span style="color: #008080;">23</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'gc'<span style="color: #000000;">)</span><span style="color: #008080;">24</span> <span style="color: #000000;"> );</span><span style="color: #008080;">25</span> <span style="color: #008080;">session_start</span><span style="color: #000000;">();</span><span style="color: #008080;">26</span> <span style="color: #000000;"> }</span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> open(<span style="color: #800080;">$path</span>,<span style="color: #800080;">$name</span><span style="color: #000000;">){</span><span style="color: #008080;">29</span> <span style="color: #008080;">30</span> <span style="color: #000000;"> }</span><span style="color: #008080;">31</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> close(){</span><span style="color: #008080;">32</span> <span style="color: #008080;">33</span> <span style="color: #000000;"> }</span><span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> read(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">){</span><span style="color: #008080;">36</span> <span style="color: #800080;">$val</span> = self::<span style="color: #800080;">$handler</span>->get(self::session_key(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">));</span><span style="color: #008080;">37</span> <span style="color: #008080;">38</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$val</span>===<span style="color: #0000ff;">false</span> || <span style="color: #800080;">$val</span>==<span style="color: #0000ff;">null</span><span style="color: #000000;">){</span><span style="color: #008080;">39</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;</span><span style="color: #008080;">40</span> <span style="color: #000000;"> }</span><span style="color: #008080;">41</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$val</span><span style="color: #000000;">;</span><span style="color: #008080;">42</span> <span style="color: #000000;"> }</span><span style="color: #008080;">43</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> write(<span style="color: #800080;">$PHPSESSID</span>,<span style="color: #800080;">$data</span><span style="color: #000000;">){</span><span style="color: #008080;">44</span> <span style="color: #800080;">$method</span> = <span style="color: #800080;">$data</span>? 'set':'replace'<span style="color: #000000;">;</span><span style="color: #008080;">45</span> <span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$handler</span>-><span style="color: #800080;">$method</span>(self::session_key(<span style="color: #800080;">$PHPSESSID</span>),<span style="color: #800080;">$data</span>,MEMCACHE_COMPRESSED,self::<span style="color: #800080;">$lifetime</span><span style="color: #000000;">);</span><span style="color: #008080;">46</span> <span style="color: #000000;"> }</span><span style="color: #008080;">47</span> <span style="color: #008080;">48</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> destroy(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">){</span><span style="color: #008080;">49</span> <span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$handle</span>->delete(self::session_key(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">));</span><span style="color: #008080;">50</span> <span style="color: #000000;"> }</span><span style="color: #008080;">51</span> <span style="color: #008000;">//</span><span style="color: #008000;">memcache本身就有限定时间,数据自动销毁,所以可不使用gc方法</span><span style="color: #008080;">52</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> gc(<span style="color: #800080;">$lifetime</span><span style="color: #000000;">){</span><span style="color: #008080;">53</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">54</span> <span style="color: #000000;"> }</span><span style="color: #008080;">55</span> <span style="color: #008080;">56</span> <span style="color: #008000;">//</span><span style="color: #008000;">给sessionID加前缀,避免key重复</span><span style="color: #008080;">57</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> session_key(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">){</span><span style="color: #008080;">58</span> <span style="color: #800080;">$session_key</span> = self::MS.<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">;</span><span style="color: #008080;">59</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$session_key</span><span style="color: #000000;">;</span><span style="color: #008080;">60</span> <span style="color: #000000;"> } </span><span style="color: #008080;">61</span> <span style="color: #000000;"> }</span><span style="color: #008080;">62</span> <span style="color: #800080;">$mem</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Memcache;</span><span style="color: #008080;">63</span> <span style="color: #800080;">$mem</span>->connect("localhost",11211) or <span style="color: #0000ff;">die</span>("could not connect"<span style="color: #000000;">);</span><span style="color: #008080;">64</span> MemSession::start(<span style="color: #800080;">$mem</span>);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

The problem was found in the springboot project production session-out timeout. The problem is described below: In the test environment, the session-out was configured by changing the application.yaml. After setting different times to verify that the session-out configuration took effect, the expiration time was directly set to 8 hours for release. Arrived in production environment. However, I received feedback from customers at noon that the project expiration time was set to be short. If no operation is performed for half an hour, the session will expire and require repeated logins. Solve the problem of handling the development environment: the springboot project has built-in Tomcat, so the session-out configured in application.yaml in the project is effective. Production environment: Production environment release is

Solution to the problem that the php session disappears after refreshing: 1. Open the session through "session_start();"; 2. Write all public configurations in a php file; 3. The variable name cannot be the same as the array subscript; 4. In Just check the storage path of the session data in phpinfo and check whether the sessio in the file directory is saved successfully.

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

The default expiration time of session PHP is 1440 seconds, which is 24 minutes, which means that if the client does not refresh for more than 24 minutes, the current session will expire; if the user closes the browser, the session will end and the Session will no longer exist.

Problem: Today, we encountered a setting timeout problem in our project, and changes to SpringBoot2’s application.properties never took effect. Solution: The server.* properties are used to control the embedded container used by SpringBoot. SpringBoot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use server.* properties to configure the controlled servlet container (tomcat, jetty, etc.). When the application is deployed as a war file to a Tomcat instance, the server.* properties do not apply. They do not apply,
