欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-23 08:08:48
...
【学习笔??®??】session机制实çŽ??PHP??´­ç‰©??½¦

使ç”??åˆ??sessionçš„??¡µ??¢å¿…??¡»??¦æœ‰session启åŠ??函æ•??session_start();清空系统中session可以ç”??session_destroy();函æ•??。

åœ??php.ini文件中关于sessionçš„??…ç½®ï¼šsession.save_path定义了存放session çš„????¯å¾„ï¼›session.name定义了sessionID的名ç????。

下??¢æ˜¯ç»ƒä¹ çš„例子,列出代码。

index.html??¡µ??¢ï¼š

	商品分ç??»åˆ—??¡????¿åŠ??å™??材
办公�?品
烟??…’副??£Ÿ
查看??´­ç‰©??½¦

文件a.php??¡µ??¢ä»£ç ï¼š

??´­ç‰©??¡—	??¯????€‰æ‹©å•†å“
篮球
排球
??¶³çƒ
桌球
æ??”球

文件b.php??¡µ??¢ä»£ç ï¼š

	??´­ç‰©??¡—	??¯????€‰æ‹©å•†å“
??“…笔
??’¢ç¬”
圆珠笔
电笔
??‡ªåŠ??笔

文件c.php??¡µ??¢ä»£ç ï¼š

	??´­ç‰©??¡—	??¯????€‰æ‹©å•†å“
猪??‚‰
牛??‚‰
中华??¦™çƒŸ
??•¿åŸŽ??‘¡??„??…’
??‡‘??¾™????¼??Š??生油

index.phpæ•??据å??„理??¡µ??¢ï¼š

 $value)		statement	*/	if(isset($_POST['d'])){				//是否从??´­ç‰©??½¦ç®¡ç†ç•Œ??¢æäº?訨¿‡æ¥çš„		foreach($_POST['d'] as $c){		//如果是,则å??†æäº?訨¿‡æ¥çš„商品序å??从??´­ç‰©??½¦æ•??组中删??™??			unset($_SESSION['cart'][$c]);		}		}?>??¿åŠ??å™??材
办公�?品
烟??…’副??£Ÿ
查看??´­ç‰©??½¦

??´­ç‰©??½¦ç®¡ç†??¡µ??¢cart.php,??¿™??‡Œä¸»??¦å®žçŽ??删??™??商品、æ’?訦”€??´­ç‰©??¡µ??¢ï¼š


$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


【学习札??®??】session机制实çŽ??PHP??´­ç‰©??½¦

å£??明:本文内容ç”??网友??‡ªå‘??´¡çŒ®ï¼Œç‰ˆæƒå½’原作??€…所有,本站不承担相应法律??´£ä»»ã€‚如æ‚??发çŽ??有涉嫌抄??¢­ä¾µæƒçš„内容,??¯????”ç³»admin@php.cn核实å??„理。

相关文章

相关????†??¢‘