PHP session-Simulating the function of a shopping cart

Source: Internet
Author: User

1, PHP default is not to open the session, to use the session in two ways: (1) use Session_Start (), display the open session. (2) Find the following line in php.ini: Find Session.auto_start = 0, set 0 to 1. The following is a simulation of the shopping cart program to show the basic operation of the seeion.

session1.php

1<?PHP2 //session_start ();3?>4<! DOCTYPE html>567<title>storing anArrayWith a session</title>89<body>Ten One<?PHP A if(isset($_post[' Form_products '])){ -     if(!Empty($_session[' Products '])){ -         $products=Array_unique( the             Array_merge(unserialize($_session[' Products ']),$_post[' Form_products '])); -             $_session[' Products ']=Serialize($products); -     } -     Else{ +         $_session[' Products ']=Serialize($_post[' Form_products ']); -     } +     Echo"<p>your Products has been registered!</p>"; A } at?> -<form method= "POST" action= "<?php Echo$_server[' php_self '];?> "> -<p> -<label for= "Form_products" >select some products:</label><br/> -<select id= "form_products" name= "form_products[" "multiple=" multiple "size=" 3 "> -<option value= "Sonic screwdriver" >sonic screwdriver</option> in<option value= "Hal" >hal 2000</option> -<option value= "Tardis" >Tardis</option> to<option value= "Transportor" >Transportor</option> +</select> -</p> the<button type= "Submit" name= "Submit" value= "Choose" >submit form</button> *</form> $<p><a href= "session2.php" >go to content page</a> </p>Panax Notoginseng</body> -

session2.php

1<! DOCTYPE html>234<title>storing anArrayWith a session</title>56<body>78<?PHP9 if(isset($_post[' Form_products '])){Ten     if(!Empty($_session[' Products '])){ One         $products=Array_unique( A             Array_merge(unserialize($_session[' Products ']),$_post[' Form_products '])); -             $_session[' Products ']=Serialize($products); -     } the     Else{ -         $_session[' Products ']=Serialize($_post[' Form_products ']); -     } -     Echo"<p>your Products has been registered!</p>"; + } -?> +<form method= "POST" action= "<?php Echo$_server[' php_self '];?> "> A<p> at<label for= "Form_products" >select some products:</label><br/> -<select id= "form_products" name= "form_products[" "multiple=" multiple "size=" 3 "> -<option value= "Sonic screwdriver" >sonic screwdriver</option> -<option value= "Hal" >hal 2000</option> -<option value= "Tardis" >Tardis</option> -<option value= "Transportor" >Transportor</option> in</select> -</p> to<button type= "Submit" name= "Submit" value= "Choose" >submit form</button> +</form> -<p><a href= "session2.php" >go to content page</a> </p> the</body> *

PHP session-Simulating the function of a shopping cart

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.