日期:2014-05-17  浏览次数:20494 次

【学习笔记】session机制实现PHP购物车

使用到session的页面必须要有session启动函数session_start();清空系统中session可以用session_destroy();函数。

在php.ini文件中关于session的配置:session.save_path定义了存放session 的路径;session.name定义了sessionID的名称。

下面是练习的例子,列出代码。

index.html页面:

<html>
	<head>
		<title>商品分类列表</title>
	</head>
	<body>
		<a href="a.php">运动器材</a><br/>
		<a href="b.php">办公用品</a><br/>
		<a href="c.php">烟酒副食</a><br/>
		<a href="cart.php">查看购物车</a><br/>
	</body>
</html>

文件a.php页面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=GBK" http-equiv="Content-Type" />
<title>购物街</title>
</head>

<body bgcolor="#fff" text="#000" link="#f96" vlink="#f96" alink="#fc9">
	请选择商品<br />
	<form action="index.php" method="POST">
		<input type="checkbox" name="cart[ ]" value="篮球"/>篮球<br />
		<input type="checkbox" name="cart[ ]" value="排球"/>排球<br />
		<input type="checkbox" name="cart[ ]" value="足球"/>足球<br />
		<input type="checkbox" name="cart[ ]" value="桌球"/>桌球<br />
		<input type="checkbox" name="cart[ ]" value="气球"/>气球<br />
		<input type="submit" value="购买" />
		<input type="button" value="返回" onclick="location='index.php';" />
	</form>
</body>

</html>

文件b.php页面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>购物街</title>
</head>

<body bgcolor="#fff" text="#000" link="#f96" vlink="#f96" alink="#fc9">
	请选择商品<br />
	<form action="index.php" method="POST">
		<input type="checkbox" name="cart[]" value="铅笔"/>铅笔<br />
		<input type="checkbox" name="cart[]" value="钢笔"/>钢笔<br />
		<input type="checkbox" name="cart[]" value="圆珠笔"/>圆珠笔<br />
		<input type="checkbox" name="cart[]" value="电笔"/>电笔<br />
		<input type="checkbox" name="cart[]" value="自动笔"/>自动笔<br />
		<input type="submit" value="购买" />
		<input type="button" value="返回" onclick="location='index.php';" />
	</form>

</body>
</html>

文件c.php页面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>购物街</title>
</head>

<body bgcolor="#fff" text="#000" link="#f96" vlink="#f96" alink="#fc9">
	请选择商品<br />
	<form action="index.php" enctype="multipart/form-data" method="POST">
		<input type="checkbox" name="cart[]" value="猪肉"/>猪肉<br />