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

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

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
