"Learning Notes" session mechanism to implement PHP shopping cart

Source: Internet
Author: User
"Learning Notes" session mechanism to implement PHP shopping cart

Use the session to the page must have the session start function session_start (), emptying the system in the session can be used Session_destroy ();

The configuration of the session in the php.ini file: Session.save_path defines the path where the session is stored, and Session.name defines the name of the SessionID.

Here is an example of the practice, listing the code.

Index.html page:

Product Category ListSports Equipment
Office Supplies
Tobacco and alcohol staple
View Shopping Cart

File a.php page code:

 
  Shopping streetPlease select a product

File b.php page code:

Shopping streetPlease select a product

File c.php page code:

Shopping streetPlease select a product

index.php Data processing page:

 $value) statement*/if (isset ($_post[' d ')) {//whether the foreach ($_post[' d ') as $c) {//is submitted from the shopping Cart management interface {///if yes, Delete the unset ($_session[' cart ' [$c]) from the shopping Cart array by submitting the item serial number;}}? > Sports Equipment
Office Supplies
Tobacco and alcohol staple
View Shopping Cart

Shopping Cart Management page cart.php, where the main implementation of the deletion of goods, the revocation of the shopping page:


Notice the setting of the Enctype property in the form form in PHP.

The Enctype property in the form form specifies the type of encoding the browser uses when sending data back to the server.
Here is the value description:
Multipart/form-data: The form data is encoded as a message, and each control on the page corresponds to a part of the message and does not encode the character. This value is required when you use a form that has a file upload control ...
application/x-www-form-urlencoded: The form data is encoded as a name/value pair. This is the standard encoding format. Encode all characters before sending (default).
Text/plain: Form data is encoded in plain text with no control or formatting characters. Converts a space to a "+" symbol, but does not encode special characters. Capture packet visible data form.

Description

1) If there is a file in the form to upload, the form label must be set enctype= "Multipart/form-data" to ensure the MIME encoding of the anonymous upload file. By default, the form's encoding format is application/x-www-form-urlencoded and cannot be used for file uploads;

2) If the form is set enctype= "Text/plain" when the session is processed, the value cannot be obtained by $_post,$_get,$_request etc. This must be careful!!!

For further explanations of three types, the substantive reference is as follows:

Enctype defines how the data should is formatted before sending. The correct formats is 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 to a distinct section of data). Text/plain means nothing should being done-its behaviour is essentially undefined between browsers, and were only ever used For automated e-mail 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 fo R 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.%0 d%0athe+second+line.%0 d%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--
Can see how the first one are useful for passing values unchecked directly to an e-mail program (e.g. if your form use S a mailto:address, so it uses your visitor ' s e-mail program to send) but was largely useless for programmatic access to fi ELDs. Only the bottom 2 examples is valid for actual form-based form data


  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.