• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-24) 73次浏览 已收录 0个评论

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

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

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

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

index.html页面:

			商品分类列表		<body>		运动器材
办公用品
烟酒副食
查看购物车

文件a.php页面代码:

<meta content="text/html; charset=GBK" http-equiv="Content-Type" />购物街<body bgcolor="#fff" text="#000" link="#f96" vlink="#f9*本文来@源gao@daima#com搞(%代@#码@网2
搞代gaodaima码

6" alink="#fc9"> 请选择商品
篮球
排球
足球
桌球
气球

文件b.php页面代码:

	购物街<body bgcolor="#fff" text="#000" link="#f96" vlink="#f96" alink="#fc9">	请选择商品
铅笔
钢笔
圆珠笔
电笔
自动笔

文件c.php页面代码:

	购物街<body bgcolor="#fff" text="#000" link="#f96" vlink="#f96" alink="#fc9">	请选择商品
猪肉
牛肉
中华香烟
长城葡萄酒
金龙鱼花生油

index.php数据处理页面:

<?php	//三个页面虽然显示内容不同,但是处理的php页面都是列表的index.php页面。处理页面会开启session功能,存储session的值。	session_start();	//启用session	if(!isset($_SESSION['cart'])){	//查看当前session中是否已经定义了购物车变量		$_SESSION['cart'] = array();	//没有的话就新建一个变量,其值是一个空数组。 		//session销毁之后变为空	}	if(isset($_POST['cart'])){		//是否是从商品页面提交过来的		//如果是,则把提交来的商品加到购物车中	//	$_SESSION['cart'] = array_merge($_SESSION['cart'],$_POST['cart']);		//通过foreach遍历数组,把前台页面的值存储在session中。如果我们希望查看购物车,也就是查看session中存储的值。cart.php页面处理查看购物车	//	foreach($_POST['cart'] as $c){	//		$_SESSION['cart'][]=$c;	//	}			//定义关联数组,其键为商品名称,其值为商品数量。第一次买进的商品,其值为1		for($i = 0; $i  $value)		statement	*/	if(isset($_POST['d'])){				//是否从购物车管理界面提交过来的		foreach($_POST['d'] as $c){		//如果是,则将提交过来的商品序号从购物车数组中删除			unset($_SESSION['cart'][$c]);		}		}?>运动器材
办公用品
烟酒副食
查看购物车

购物车管理页面cart.php,这里主要实现删除商品、撤销购物页面:

<?php	session_start();			//重启session	$cart = $_SESSION['cart'];	//得到购物车	foreach($cart as $i=>$c){	//对购物车里的商品进行遍历		//将商品的名字输出到页面上,每个商品前面对应一个多选框,其值是商品在购物车中的编号。		//用d作为购物车管理界面中  购物车所有的商品,用于index.php页面撤销/删除某些商品的业务处理。		echo "".$i.' 数量:'.$c."
"; }?>

这里要注意php中form表单中enctype属性的设置。

form表单中的enctype属性指定将数据发回到服务器时浏览器使用的编码类型。
下面是取值说明:
multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,不对字符编码。当使用有文件上传控件的表单时,该值是必需的。。
application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。在发送前对所有字符进行编码(默认)。
text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。将空格转换为 “+” 符号,但不编码特殊字符。抓包可见数据形式。

说明:

1)如果表单中有文件要上传,表单中form标签必须设置enctype=”multipart/form-data”来确保匿名上传文件的MIME编码。默认情况下,表单的编码格式是 application/x-www-form-urlencoded,不能用于文件上传;

2)进行session处理时,如果将表单设置enctype=”text/plain”,则通过$_POST,$_GET,$_REQUEST等是无法取到值的。这点一定要小心!!!

对于三种类型的进一步解释,实质参考如下:

enctype defines how the data should be formatted before sending. the two correct formats are application/x-www-form-urlencoded (which essentially sends things as key=valye&anotherkey=anothervalue, with http headers) and multipart/form-data (which splits each key up into a distinct section of data). text/plain means nothing should be done – its behaviour is essentially undefined between browsers, and was only ever used for automated email forms in the days before spam. use application/x-www-form-urlencoded for text forms (or leave this to use the default setting) and multipart/form-data for file attachment uploads

text/plain:

Content-Type: text/plain================================================================================foo=barbaz=The first line.The second line.

application/x-www-form-urlencoded:

Content-Type: application/x-www-form-urlencoded================================================================================foo=bar&baz=The+first+line.%0D%0AThe+second+line.%0D%0A

multipart/form-data:

Content-Type: multipart/form-data; boundary=---------------------------314911788813839================================================================================-----------------------------314911788813839Content-Disposition: form-data; name="foo"bar-----------------------------314911788813839Content-Disposition: form-data; name="baz"The first line.The second line.-----------------------------314911788813839--

you can see how the first one is useful for passing values UNCHECKED directly into an email program (e.g. if your form uses a mailto: address, so it uses your visitor’s email program to send) but is largely useless for programmatic access to fields. only the bottom 2 examples are valid for actual form-based form data


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:【学习札记】session机制实现PHP购物车

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址