PHP学习笔记14-操作session

时间:2020-11-27 08:47:44

PHP会话管理图:

PHP学习笔记14-操作session

创建index:

 <?php
 /**
  * Created by PhpStorm.
  * User: Administrator
  * Date: 2015/7/2
  * Time: 16:28
  */

 session_start();//启用session
 $_SESSION['name'] = 'Vito';//写session数据

创建a.php访问index.php的数据:

 <?php
 /**
  * Created by PhpStorm.
  * User: Administrator
  * Date: 2015/7/2
  * Time: 16:35
  */

 session_start();
 echo $_SESSION['name'];//访问index.php数据

修改index.php增加跳转:

 header("Location:a.php");

访问index.php会跳转到a.php并输出Vito

销毁session

修改a.php增加判断:

 <?php
 /**
  * Created by PhpStorm.
  * User: Administrator
  * Date: 2015/7/2
  * Time: 16:35
  */

 session_start();
 //echo $_SESSION['name'];//访问index.php数据

 //清除session数据
 if(isset($_SESSION['name'])){
     echo $_SESSION['name'];
 }else{
     echo "No name found";
 }

修改index.php增加session_destroy()

 <?php
 /**
  * Created by PhpStorm.
  * User: Administrator
  * Date: 2015/7/2
  * Time: 16:28
  */

 session_start();//启用session
 $_SESSION['name'] = 'Vito';//写session数据
 session_destroy();//销毁session
 header("Location:a.php");
 //echo session_id();//当前用户的sessionID;服务器端用sessionID标识每个客户端
 //浏览器完全退出后,再次开启浏览器会得到一个不同的sessionID

访问index.php会得到No name found