Home > Backend Development > PHP Tutorial > Detailed explanation and example code of how to use PHP Session variables

Detailed explanation and example code of how to use PHP Session variables

高洛峰
Release: 2023-03-04 08:20:02
Original
2124 people have browsed it

Detailed explanation and example code of how to use PHP Session variables

When you run an app, 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.

Related topic recommendations: php session (including pictures, texts, videos, cases)

Copy the manual, then try each one and write it out for your own reference. Who told us to just learn it? Woolen cloth. Session has about 12 functions:

  • session_start: initial session.

  • session_destroy: End session.

  • session_unset: Release session memory.

  • session_name: access the current session name.

  • session_module_name: access the current session module.

  • session_save_path: access the current session path.

  • session_id: access the current session code.

  • session_register: Register new variables.

  • session_unregister: Delete registered variables.

  • session_is_registered: Check whether the variable is registered.

  • session_decode: Session data decoding.

  • session_encode: Session data encoding.

There is also a global variable: $_SESSION

Before you store user information in the PHP session, you must first start the session.
Note: session_start() The function must be placed before the label:

<?php session_start(); ?>

<html>
<body>

</body>
</html>
Copy after login

Storage Session variables

<?php
session_start();
// store session data
$_SESSION[&#39;views&#39;]=1;
?> 
<html>
<body>

<?php
//retrieve session data
echo "Pageviews=". $_SESSION[&#39;views&#39;];
?>

</body>
</html>
 [html]
终结 Session
unset() 函数用于释放指定的 session 变量:
[code]
<?php
unset($_SESSION[&#39;views&#39;]);
?>
Copy after login

You can also completely terminate the session through the session_destroy() function:

<?php
session_destroy();
?>
Copy after login

Example:

<?php 
session_start(); 
switch ( $_GET[&#39;action&#39;] ){ 
case "loginif"; 
//登陆验证,假定session储存的秘密应该等于123才为正确 
if ($_SESSION[&#39;pass&#39;]=="123"){echo "密码正确 您可以执行注销";}else{echo "密码错误,您可以重新登陆";} 
break; 
case "logout"; 
//注销登陆 
session_unset(); 
session_destroy(); 
echo "注销成功!可以判断一下密码是否正确来看看是不是成功注销"; 
break; 
case "login"; 
//写入session以供验证, 
$pass="123";//密码 
$_SESSION[&#39;pass&#39;]=$pass; 
echo "写入登陆密码了 去判断密码成功与否吧。"; 
break; 
} 
?> 
<p>假定本页名为temp.php </p> 
<p><a href="temp.php?action=login">用户进行登陆post,程序处理写入session</a></p> 
<p><a href="temp.php?action=loginif">判断用户密码是否正确</a></p> 
<p><a href="temp.php?action=logout">登陆成功的用户注销登陆</a></p>
Copy after login

I summarized the session in php usage.

(1) Start session
Before each use of session, add this sentence: "session_start();". As the name suggests, the function of this function is to start using the session.
(2) Register session
First, you must create a global (note, it must be defined as global, otherwise it cannot be used on other pages) array, such as $login, where $login['name']="Victor", $login[ 'pwd']="111111", and then call the function "session_register(login);", the session is successfully registered.
(3) Using variables in the session
Similar to registering a session, you must first create a global array, and then it is the same as using a normal array.
(4) Determine whether the session is registered
It is very simple, just use "if (session_is_registered(login))" to judge.
(5) Uninstalling the session
  is also very simple, just "session_unregister(login);".
Note: Be sure to do (1) before doing (2) (3) (4) (5).例 The following example is given:

index.htm

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false">&lt;html&gt; &lt;head&gt; &lt;title&gt;测试&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;FORM METHOD=POST ACTION=&quot;login.php&quot;&gt; 用户名:&lt;INPUT TYPE=&quot;text&quot; NAME=&quot;name&quot;&gt;&lt;br/&gt; 密码:&lt;INPUT TYPE=&quot;password&quot; name=&quot;pwd&quot;&gt;&lt;br/&gt; &lt;INPUT TYPE=&quot;submit&quot; value=&quot;提交&quot;&gt; &lt;/FORM&gt; &lt;/body&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div>e

login.php

rreeee

info.php

rreeeee

logout.php

<?php 
global $login; 
if ($_POST[&#39;name&#39;]!="Victor" || $_POST[&#39;pwd&#39;]!="111111") 
{ 
        echo "登陆失败"; 
        echo "请<a href=index.htm>返回</a>"; 
        exit; 
} 
$login = array(&#39;name&#39;=>$_POST[&#39;name&#39;], 
                           &#39;pwd&#39;=>$_POST[&#39;pwd&#39;]); 
session_start(); 
session_register(login); 
echo "<a href=info.php>查看信息</a><br/>"; 
echo "<a href=logout.php>退出登陆</a><br/>"; 
?>
Copy after login
More PHP Session variables How to use the way to use. For articles related to detailed explanations and example codes, please pay attention to the PHP Chinese website!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template