Home Backend Development PHP Tutorial PHP Session 变量的使用方法详解与范例代码(转)

PHP Session 变量的使用方法详解与范例代码(转)

Jun 13, 2016 pm 12:08 PM
gt login lt php session

PHP Session 变量的使用方法详解与实例代码(转)


? ? ? ? 在php中Session经常用来验证用户注册或登录之后的验证了,下面我来总结session变量的一些常用实例与用法介绍
?
?

当您运行一个应用程序时,您会打开它,做些更改,然后关闭它。这很像一次会话。计算机清楚你是谁。它知道你何时启动应用程序,并在何时终止。但是在因特网上,存在一个问题:服务器不知道你是谁以及你做什么,这是由于 HTTP 地址不能维持状态。
通过在服务器上存储用户信息以便随后使用,PHP session 解决了这个问题(比如用户名称、购买商品等)。不过,会话信息是临时的,在用户离开网站后将被删除。如果您需要永久储存信息,可以把数据存储在数据库中。

把手册抄一下,然后每个都试试然后写出来,方便自己查阅滴,谁让咱刚学呢。Session大概有12个函数分别是:

session_start: 初始 session。
session_destroy: 结束 session。
session_unset: 释放session内存。
session_name: 存取目前 session 名称。
session_module_name: 存取目前 session 模块。
session_save_path: 存取目前 session 路径。
session_id: 存取目前 session 代号。
session_register: 注册新的变量。
session_unregister: 删除已注册变量。
session_is_registered: 检查变量是否注册。
session_decode: Session 资料解码。
session_encode: Session 资料编码。

还有个全局变量就是:$_SESSION


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

?

复制代码代码如下:








?

?

存储 Session 变量

?

复制代码代码如下:

session_start();
// store session data
$_SESSION['views']=1;
?>?



//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>



?[html]

?

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

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

?

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

?

复制代码代码如下:

session_destroy();
?>
?

?

实例:

?

复制代码代码如下:

session_start();?
switch ( $_GET['action'] ){?
case "loginif";?
//登陆验证,假定session储存的秘密应该等于123才为正确?
if ($_SESSION['pass']=="123"){echo "密码正确 您可以执行注销";}else{echo "密码错误,您可以重新登陆";}?
break;?
case "logout";?
//注销登陆?
session_unset();?
session_destroy();?
echo "注销成功!可以判断一下密码是否正确来看看是不是成功注销";?
break;?
case "login";?
//写入session以供验证,?
$pass="123";//密码?
$_SESSION['pass']=$pass;?
echo "写入登陆密码了 去判断密码成功与否吧。";?
break;?
}?
?>?

假定本页名为temp.php

?

用户进行登陆post,程序处理写入session

?

判断用户密码是否正确

?

登陆成功的用户注销登陆


?

?

我总结了一下php中session的用法。

(一)开始session?
  在每一次使用session之前,都要加上这一句:“session_start();”。顾名思义,这个函数的作用就是开始使用session。?
(二)注册session?
  首先要建立一个global(注意,一定要定义为global,不然在其它页面用不了)数组,如$login,其中$login['name']="Victor",$login['pwd']="111111",然后调用函数“session_register(login);”,session就成功注册了。?
(三)使用session里面的变量?
  和注册session类似,都要先建立一个global数组,然后就和使用一般数组一样了。?
(四)判断session是否注册?
  很简单,用“if (session_is_registered(login))”判断就可以了。?
(五)卸载session?
  也很简单,“session_unregister(login);”就可以了。?
  注意:在进行(二)(三)(四)(五)之前一定要先进行(一)。


下面给出一个例子:

index.htm

?

复制代码代码如下:

?
?
测试?
?
?
?
用户名:
?
密码:
?
?
?
?

?

?

login.php

?

复制代码代码如下:

global $login;?
if ($_POST['name']!="Victor" || $_POST['pwd']!="111111")?
{?
??????? echo "登陆失败";?
??????? echo "请返回";?
??????? exit;?
}?
$login = array('name'=>$_POST['name'],?
?????????????????????????? 'pwd'=>$_POST['pwd']);?
session_start();?
session_register(login);?
echo "查看信息
";?
echo "退出登陆
";?
?>
?

?

info.php

?

复制代码代码如下:

session_start();?
if (session_is_registered(login))?
{?
??????? global $login;?
??????? echo "hello,".$login['name']."
";?
??????? echo "退出登陆
";?
}?
else?
{?
??????? echo "非法操作
";?
??????? exit;?
}?
?>

?


logout.php

?

复制代码代码如下:

session_start();?
session_unregister(login);?
header("location:index.htm");?
?>
?

?

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles