PHP Session 变量的使用方法详解与实例代码_php技巧
当您运行一个应用程序时,您会打开它,做些更改,然后关闭它。这很像一次会话。计算机清楚你是谁。它知道你何时启动应用程序,并在何时终止。但是在因特网上,存在一个问题:服务器不知道你是谁以及你做什么,这是由于 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
我总结了一下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");
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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