Summary of PHP Session

【Foreword】

   This article mainly summarizes the knowledge related to Session in PHP   

 

【Introduction】

   When using PHP to apply a session, store the data in the session on the server, and then identify the client's information through the sessionID sent by the client, and extract the information.

   Common operations of session in php: session writing, reading, registration and deletion.

(1) Initialization of session

   The function that marks the start of session usage is session_start, which is used to initialize session variables. The syntax is as follows:

session_start();

   The return value is true

(2) session writing and reading

   In PHP, the use of session is done by calling and reading the superglobal variable $_SESSION

  

【List】

    (1) Introduction; (2) Origin; (3) Working mechanism; (4) Open session;

    (5) Store and obtain; (6) Store and obtain Session cases; (7) Delete/release Session

 

【Details】

 (1 Introduction

   session variables are used to store information about a user's session, or to change settings for a user's session. Session variables hold information that is single-user and available to all pages in the application

(2) Origin

   When running an app, you open it, make some changes, and 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. But on the Internet, there's a problem: the server doesn't know who you are and what you do, because HTTP addresses don't maintain state.

   PHP sessions solve this problem by storing user information on the server for later use (such as username, purchases, 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

(3) Working mechanism

   Create a unique id (UID) for each visitor and store variables based on this UID. UIDs are stored in cookies or transmitted via URL

(4) Open a session

   在把用户信息存储到session中之前,首先必须启动会话。启动会话session_start() 函数必须位于 <html> 标签之前:

<?php session_start(); ?>
<html>
<body>...</body>
</html>

   上面的代码会向服务器注册用户的会话,以便开始保存用户信息,同时会为用户会话分配一个UID

(5)存储和获取

   用$_SESSION超全局变量可以存储和获取session变量

<?php
session_start();
$_SESSION['views']=1;// 存储Session
?>
<html>
<body>
<?php
echo "Pageviews=". $_SESSION['views'];//获取Session
?>
</body>
</html>

   注意:在其他页面获取前,首先要启动会话session-start();

(6)存储获取Session案例

   下面创建一个简单的页面计数器案例

   isset() 函数检测是否已设置 "views" 变量,如果已设置 "views" 变量,便累加计数器。如果 "views" 不存在,则创建 "views" 变量,并把它设置为 1:

<?php
session_start();
if(isset($_SESSION['views'])){
  $_SESSION['views']=$_SESSION['views']+1;
}else{
  $_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
}
?>

 (7)删除 / 释放Session

   如果想删除某些session数据,可以使用unset()或session_destroy()函数

   unset() 函数用于释放指定的 session 变量:

<?php
unset($_SESSION['views']);
?>

   也可以通过 session_destroy() 函数彻底终结 session:

<?php
session_destroy();
?>

   区别:

   ①unset() 函数用于释放指定的 session 变量

   ②session_destroy()将重置session,失去所有已存储的 session 数据

针对这个区别,我做了一个案例

<?php
session_start();
?>
</head>
if(isset($_SESSION['views']))
  $_SESSION['views']=$_SESSION['views']+1;
else
  $_SESSION['views']=1;
  echo "数量=". $_SESSION['views']."<br>";
?>
<?php
$_SESSION['name'] = 'Tony';
echo "名字是=". $_SESSION['name']."<br>";
?>
<a href="./demo.php">页面2</a>
<?php session_start(); ?>
<a href="./index.php">链接</a><br>
<?php
echo "数量=". $_SESSION['views']."<br>";
echo "名字是=". $_SESSION['name'];
?>
<?php
//unset($_SESSION['views']);
//unset($_SESSION['name']);
session_destroy();
?>

 


 

【案例】

    (1)注册页面:

   在网站的页面中,在注册页面对$_SESSION数组进行赋值,在其他的页面中对$_SESSION数组进行读取

   注册页面中的session,例如:

<?php
session_start();
$_SESSION['Name']= "Tony";
?>

   其他页面中的session,例如:

<?php
session_start();
echo $_SESSION['Name'];
?>

   依次运行,结果是:Tony

(2)投票

<?php
if(isset($_POST['submit'])){
    session_start();                                //开始建立一个会话
    $_SESSION['season'] = $_POST['season'];       //存储会话数据
    header("Location: demo2.php");            //应特别注意header()里的格式问题
}
?>
<b>存储会话</b>
<hr/>
选择需要设置的数据:
<form name="form1" method="post" action="" id="form1" >
    <select name="season" id="season_select" >
        <option value="">无数据</option>
        <option value="春天">春天</option>
        <option value="夏天">夏天</option>
        <option value="秋天">秋天</option>
        <option value="冬天">冬天</option>
    </select>
    <br/>
    <br/>
    <br/>
   <input type="submit" name="submit" value="submit"/>
</form>
<?php
session_start();    //建立或者继续一个会话
$season = $_SESSION['season'];       //读取会话数据
echo "<b>读取会话</b><br/><br/>";
switch ($season) {
    case '春天';
        echo '现在是绿意盎然的春天!';
        break;
    case '夏天';
        echo '现在是热情四溢的夏天!';
        break;
    case '秋天';
        echo '现在是丰收果实的秋天!';
        break;
    case '冬天';
        echo '现在是白雪皑皑的冬天!';
        break;
    default ;
        echo '对不起,会话中没有数据  或者  不存在该对话 !';
}
?>

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326182155&siteId=291194637