Session 的功能是跟踪用户状态,以保护用户请求的过程。
大家可能用 session 保存变量和数组都用过,其实用 Session 保存对象的功能更强大,
可以大大减少开发者的时间,需要注意的是 php 的 Session 只能保存对象不能保存引用,
如数据库连接和文件句柄等。
下面提供 php 实例:
首先创建一个 session.php 文件,用来创建 Web 类。
php 代码如下:
1.
<?
php
2. //创建一个 Web 类
3.
class
Web
{
4.
private
$webName
;
5.
private
$webUrl
;
6.
private
$webDescribe
;
7.
public
function
__construct
(
$webName
,
$webUrl
,
$webDescribe
){
8.
if
(!
$webName
){
9.
throw
new
Exception
(
'网站名称没赋值'
);
10.
}
11.
if
(!
$webUrl
){
12.
throw
new
Exception
(
'网站 url 没有赋值'
);
13.
}
14.
if
(!
$webDescribe
){
15.
throw
new
Exception
(
'网站描述内容没有赋值'
);
16.
}
17.
$this
->
webName
=
$webName
;
18.
$this
->
webUrl
=
$webUrl
;
19.
$this
->
webDescribe
=
$webDescribe
;
20.
}
21.
public
function
getwebName
(){
22.
return
$this
->
webName
;
23.
}
24.
public
function
getwebUrl
(){
25.
return
$this
->
webUrl
;
26.
}
27.
public
function
getwebDescribe
(){
28.
return
$this
->
webDescribe
;
29.
}
30.
}