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