码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-03-04 18:22:42      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:基本   reg   php.ini   cti   mit   for   sel   rod   方法   

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="Sonic Screwdriver">Sonic Screwdriver</option>
29         <option value="Hal 2000">Hal 2000</option>
30         <option value="Tardis">Tardis</option>
31         <option value="Transportor">Transportor</option>
32     </select>
33 </p>
34     <button type="submit" name="submit" value="choose">Submit Form</button>
35 </form>
36    <p><a href="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="Sonic Screwdriver">Sonic Screwdriver</option>
26         <option value="Hal 2000">Hal 2000</option>
27         <option value="Tardis">Tardis</option>
28         <option value="Transportor">Transportor</option>
29     </select>
30 </p>
31     <button type="submit" name="submit" value="choose">Submit Form</button>
32 </form>
33    <p><a href="session2.php">go to content page</a> </p>
34 </body>
35 </html>

 

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

标签:基本   reg   php.ini   cti   mit   for   sel   rod   方法   

原文地址:http://www.cnblogs.com/godlei/p/6501878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!