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

Dec 24, 2016 am 09:33 AM
php session

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:php;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!
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles