首页 > 代码库 > PHP会话——模拟购物车的功能

PHP会话——模拟购物车的功能

1、php默认是不开启会话的,要使用会话用两种方法:(1)使用session_start();显示的开启会话。(2)在php.ini中找到如下的一行:找到session.auto_start = 0,将0设置为1.下面通过一个模拟购物车的程序来展示seeion的基本操作。

session1.php

 1 <?php
 2 //session_start();
 3 ?>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7     <title>Storing an array with a session</title>
 8 </head>
 9 <body>
10 <h1>Product Choice Page</h1>
11 <?php
12 if(isset($_POST[‘form_products‘])){
13     if(!empty($_SESSION[‘products‘])){
14         $products = array_unique(
15             array_merge(unserialize($_SESSION[‘products‘]), $_POST[‘form_products‘]));
16             $_SESSION[‘products‘]=serialize($products);
17     }
18     else{
19         $_SESSION[‘products‘]=serialize($_POST[‘form_products‘]);
20     }
21     echo "<p>your products have been registered!</p>";
22 }
23 ?>
24 <form method="post" action="<?php echo $_SERVER[‘PHP_SELF‘]; ?>">
25 <p>
26     <label for = "form_products">Select some products:</label><br />
27     <select id="form_products" name="form_products[]" multiple="multiple" size="3">
28         <option value="http://www.mamicode.com/Sonic Screwdriver">Sonic Screwdriver</option>
29         <option value="http://www.mamicode.com/Hal 2000">Hal 2000</option>
30         <option value="http://www.mamicode.com/Tardis">Tardis</option>
31         <option value="http://www.mamicode.com/Transportor">Transportor</option>
32     </select>
33 </p>
34     <button type="submit" name="submit" value="http://www.mamicode.com/choose">Submit Form</button>
35 </form>
36    <p><a href="http://www.mamicode.com/session2.php">go to content page</a> </p>
37 </body>
38 </html>

session2.php

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>Storing an array with a session</title>
 5 </head>
 6 <body>
 7 <h1>Product Choice Page</h1>
 8 <?php
 9 if(isset($_POST[‘form_products‘])){
10     if(!empty($_SESSION[‘products‘])){
11         $products = array_unique(
12             array_merge(unserialize($_SESSION[‘products‘]), $_POST[‘form_products‘]));
13             $_SESSION[‘products‘]=serialize($products);
14     }
15     else{
16         $_SESSION[‘products‘]=serialize($_POST[‘form_products‘]);
17     }
18     echo "<p>your products have been registered!</p>";
19 }
20 ?>
21 <form method="post" action="<?php echo $_SERVER[‘PHP_SELF‘]; ?>">
22 <p>
23     <label for = "form_products">Select some products:</label><br />
24     <select id="form_products" name="form_products[]" multiple="multiple" size="3">
25         <option value="http://www.mamicode.com/Sonic Screwdriver">Sonic Screwdriver</option>
26         <option value="http://www.mamicode.com/Hal 2000">Hal 2000</option>
27         <option value="http://www.mamicode.com/Tardis">Tardis</option>
28         <option value="http://www.mamicode.com/Transportor">Transportor</option>
29     </select>
30 </p>
31     <button type="submit" name="submit" value="http://www.mamicode.com/choose">Submit Form</button>
32 </form>
33    <p><a href="http://www.mamicode.com/session2.php">go to content page</a> </p>
34 </body>
35 </html>

 

PHP会话——模拟购物车的功能