


PHP session processing function session, session function session_PHP tutorial
PHP session processing function session, session function session
PHP Session variable
When you run an application, you open it, make changes, and then close it. It's a lot like a session. The computer knows who you are. It knows when you start the application and when it terminates it. But on the Internet, there's a problem: the server doesn't know who you are and what you do, and that's because HTTP addresses don't maintain state.
PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.
Session works by creating a unique id (UID) for each visitor and storing variables based on this UID. The UID is stored in a cookie or passed through the URL.
1. Start conversation
Before saving information to the session, you must first open the session. PHP provides the session_start() function to start or continue a session. Definition:
1 bool session_start( void )
The call is as follows:
<span>1</span> <span><?</span><span>php session_start();</span><span>?></span> <span>2</span> <span><</span><span>html</span><span>></span> <span>3</span> <span><</span><span>body</span><span>></</span><span>body</span><span>></span> <span>4</span> <span></</span><span>html</span><span>></span>
Note:
(1) The session_start() function must be located before the tag , that is, the function must be called before any output. It is often careless to input too much when writing a program. A space or carriage return will result in an error. Special attention should be paid to this. (I have been tricked)
(2) Regardless of whether the session is successfully created or not, the session_start() function will return TRUE, so using any exception handling will not work .
(3) You can also enable the configuration instruction session.auto_start so that you do not have to execute this function, but in this case, a session will be started or continued when each php page is executed.
2. Store or read session
The correct way to store and read session variables is to use PHP's $_SESSION variable. $_SESSION is a global parameter provided by PHP, which is specially used to store and read sessions. (Note that the key names of associative arrays are consistent with the naming rules of ordinary variables)
When storing a session, you can assign it directly.
1 $_SESSION[‘season’] = ‘Autumn’;
The above sets a session element with the key name "season" and its value is "autumn". When reading, it is just like calling a normal array element.
The following two pieces of code show how to store and read a session element.
This is the session1.php file:
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span> * Created by PhpStorm. </span><span> 4</span> <span> * User: yuxiu </span><span> 5</span> <span> * Date: 2016/5/26 </span><span> 6</span> <span> * Time: 14:11 </span><span> 7</span> <span>*/</span> <span> 8</span> <span>if</span>(<span>isset</span>(<span>$_POST</span>['submit'<span>])){ </span><span> 9</span> <span>session_start</span>(); <span>//</span><span>开始建立一个会话</span> <span>10</span> <span>$_SESSION</span>['season'] = <span>$_POST</span>['season']; <span>//</span><span>存储会话数据</span> <span>11</span> <span>header</span>("Location: session2.php"); <span>//</span><span>应特别注意header()里的格式问题</span> <span>12</span> <span>13</span> <span>14</span> <span>} </span><span>15</span> ?> <span>16</span> <b>存储会话</b> <span>17</span> <hr/> <span>18</span> <span>选择需要设置的数据: </span><span>19</span> <form name="form1" method="post" action="" id="form1" > <span>20</span> <select name="season" id="season_select" > <span>21</span> <option value="春天">春天</option> <span>22</span> <option value="夏天">夏天</option> <span>23</span> <option value="秋天">秋天</option> <span>24</span> <option value="冬天">冬天</option> <span>25</span> </select> <span>26</span> <br/> <span>27</span> <br/> <span>28</span> <br/> <span>29</span> <input type="submit" name="submit" value="submit"/> <span>30</span> </form>
This is the session2.php file:
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span> * Created by PhpStorm. </span><span> 4</span> <span> * User: yuxiu </span><span> 5</span> <span> * Date: 2016/5/26 </span><span> 6</span> <span> * Time: 14:13 </span><span> 7</span> <span>*/</span> <span> 8</span> <span>session_start</span>(); <span>//</span><span>建立或者继续一个会话</span> <span> 9</span> <span>$season</span> = <span>$_SESSION</span>['season']; <span>//</span><span>读取会话数据</span> <span>10</span> <span>11</span> <span>echo</span> "<b>读取会话</b><br/><br/>"<span>; </span><span>12</span> <span>switch</span> (<span>$season</span><span>) { </span><span>13</span> <span>case</span> '春天'<span>; </span><span>14</span> <span>echo</span> '现在是绿意盎然的春天!'<span>; </span><span>15</span> <span>break</span><span>; </span><span>16</span> <span>case</span> '夏天'<span>; </span><span>17</span> <span>echo</span> '现在是热情四溢的夏天!'<span>; </span><span>18</span> <span>break</span><span>; </span><span>19</span> <span>case</span> '秋天'<span>; </span><span>20</span> <span>echo</span> '现在是丰收果实的秋天!'<span>; </span><span>21</span> <span>break</span><span>; </span><span>22</span> <span>case</span> '冬天'<span>; </span><span>23</span> <span>echo</span> '现在是白雪皑皑的冬天!'<span>; </span><span>24</span> <span>break</span><span>; </span><span>25</span> <span>default</span><span> ; </span><span>26</span> <span>echo</span> '对不起,会话中没有数据 或者 不存在该对话 !'<span>; </span><span>27</span> <span>} </span><span>28</span> ?>
In session1.php, first use session_start() to create a session, then use array assignment to store the submitted seasonal data, and finally use the header() function to jump directly to the beginning. In the session2.php file, the session_start() function is also needed to continue a session and use the session array to call session information.
3. Destroy session
When the session is no longer used, it needs to be destroyed manually. Although PHP has the function of automatically destroying the session, this will make the program less efficient. You can use the unset() function or session_destroy() function.
<?<span>php </span><span>unset</span>(<span>$_SESSION</span>['season'<span>]); </span>?>
Or:
<?<span>php </span><span>session_destroy</span><span>(); //注意,使用这个函数将重置session数组,即失去所有的已经储存的session数据 </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

AI Hentai Generator
Generate AI Hentai for free.

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 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

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

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.

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,

1. Implementing SMS login based on session 1.1 SMS login flow chart 1.2 Implementing sending SMS verification code Front-end request description: Description of request method POST request path /user/code request parameter phone (phone number) return value No back-end interface implementation: @Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1. Verify mobile phone number if

JavaScriptCookies Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics. PHPCookieCookies are text files that are stored on client computers and retained for tracking purposes. PHP transparently supports HTTP cookies. How do JavaScript cookies work? Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when a visitor reaches another page on the site
