当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP Sessions

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP Sessions


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1397 ::
收藏到网摘: n/a

A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.
PHP session变量的作用是:存储用户的session信息,或者改变用户的session设置。Session变量储存了一个单一用户的信息,它可以被所有的页面使用。


PHP Session Variables
PHP Session变量

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
当你在自己的计算机上运行一个应用程序时,你打开它,对他做一些改变,然后关闭它,这个过程和session很相似。计算机知道你是谁,知道你什么时候启动了应用程序,什么时候关闭了应用程序。但在互联网上,会出现这样一个问题:因为HTTP地址不可能永久的保留下来,所以服务器很难辨认你是谁,你在干什么。

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.
PHP Session允许你在服务器上储存用户信息(如:用户名[username]、购物清单[shopping]等等),从而解决了这个问题。然而,session信息也是临时存在的,当你离开这个网站时,他会被自动删除。如果你想永久保留这些信息,你可以尝试着把它储存在数据库中。

Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.
Session通过为每个访问者创建一个独立的ID(UID)并储存基于UID的变量来运行。UID存储在cookie中,或在URL中展现出来。


Starting a PHP Session
启动PHP Session

Before you can store user information in your PHP session, you must first start up the session.
在你将用户信息存入PHP Session之前,你必须先启动Session。

Note: The session_start() function must appear BEFORE the <html> tag:
注意:session_start()函数必须写在<html>标签之前:

<?php session_start(); ?>
<html>

<body>
</body>
</html>

The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.
上述代码将在服务器上注册一个用户的session,允许你储存用户信息,并为用户session指定一个UID。


Storing a Session Variable
储存一个Session变量

The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:
储存和获取session变量的最佳方法是使用PHP $_SESSION变量:

<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>

Output:
结果:

Pageviews=1

In the example below, we create a simple page-views counter. The isset() function checks if the "views" variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't exist, we create a "views" variable, and set it to 1:
在上述案例中,我们建立了一个简易的页面计数器。Isset()函数检查“views”变量是否已经被设置。如果“views”变量已经被设置了,我们会增加我们的计数。如果“views”变量不存在,我们会先创建一个“views”变量,并把“1”赋给它。

<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>

 


Destroying a Session
删除Session

If you wish to delete some session data, you can use the unset() or the session_destroy() function.
如果你希望删除一些session数据,你可以使用unset()函数或session_destroy()函数。

The unset() function is used to free the specified session variable:
Unset()函数的作用是释放指定的session变量:

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

You can also completely destroy the session by calling the session_destroy() function:
你也可以使用session_destroy()函数将session全部删除:

<?php
session_destroy();
?>

Note: session_destroy() will reset your session and you will lose all your stored session data.
注意:session_destroy()会重新设置你的session,你会丢失所有已保存的session数据。

评论 (0) All

登陆 | 还没注册?