php SESSION application example (shopping cart)

SESSION Application Example

Login example: (Please note that you must type it yourself, no CV method)
First, look at the results chart to stimulate students' desire to write. The login page is as follows:

document_2015-09-04_55e9473ab4ec7.png

Explain what the problem is. Next, implement it yourself.
First database information:
Create a new database named login, and then create a user table. The structure of the table is as follows:

document_2015-09-04_55e9487784049.png

##login.php

<?php
session_start();
if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) {
   $userName = $_POST['username'];
   $password = $_POST['password'];

   //从db获取用户信息   数据库信息改成自己的
   $conn = mysqli_connect('host','username','password','login');
   $res = mysqli_query($conn,"select * from user where `username` =  '$username' ");
   $row = mysqli_fetch_assoc($res);
   if ($row['password'] == $password) {
       //密码验证通过,设置session,把用户名和密码保存在服务端
       $_SESSION['username'] = $username;
       $_SESSION['password'] = $password;

       //最后跳转到登录后的欢迎页面 //注意:这里我们没有像cookie一样带参数过去
       header('Location: welcome.php');
   }
}

?>
<html>
<head>
<!-- 这里指明页面编码 -->
<meta charset="utf-8">
</head>
<body>
   <form action="" method="POST">
       <div>
           用户名:<input type="text" name="username" />
           密  码:<input type="text" name="password" />
           <input type="submit" value="登录">        
       </div>
   </form>
</body>
</html>

welcome.php Here we use the information in the session, instead of bringing parameters in the URL like cookies.

<?php
session_start();
$username = $_SESSION['username'];
?>
<html>
<head>

</head>
<body>
   welcome,<?php echo $username;?>
</body>
</html>

Example of shopping cart: (Please note that you must type it yourself, do not CV Dafa)

Database information: Create a database named test. There is a shop table in the library. The table structure is as follows:

document_2015-09-04_55e9496188de0.png

Start Let’s code! goodsList.php This is the product display page. The rendering is as follows:
Explain that if it is the first time to purchase an item, add the product information to the shopping cart and calculate the total price. If it is purchased again, Click to purchase, the quantity of purchased items will be increased by 1, and the total price will be recalculated. View the shopping cart link to go to the shopping cart page.

<?php
   $goods = array();
   //从数据库获取商品信息存入$goods二维数组
   $i = 0;
   //这里请换上自己的数据库相关信息
   $conn = mysqli_connect('host','username','password','test');
   $res = mysqli_query($conn,'select * from shop');
   //这里把商品信息放到$goods二维数组,每一维存的是单个
   //商品的信息,比如商品名、价格。
   while ($row = mysqli_fetch_assoc($res)) {
       $goods[$i]['id'] = $row['id'];
       $goods[$i]['name'] = $row['name'];
       $goods[$i]['price'] = $row['price'];
       $i++ ;
   }

?>
<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
   <?php
   //取出商品信息显示在页面上,并添加购买功能
       foreach ($goods as $value) {
           echo ' 商品名 ' . $value['name'] . ' 价格 ' . $value['price'];
           echo "<a href=buy.php?name=" . $value['name'] . '&price=' . $value['price'] .">购买</a>";
           echo '<br />';
       }

   ?>
   <a href="shoppingCart.php">查看购物车</a>
</body>
</html>

buy.php This page completes the purchase function and then jumps to the product list again. The main purpose is to process the purchase of goods in the session.

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<?php
   //开启session
   session_start();

   //获取传过来的商品名和价格
   $name = $_GET['name'];
   $price = $_GET['price'];

   //把session中的商品信息和传过来的(刚买的)商品信息对比
   $goods = $_SESSION['goods'];
   if ($name == $goods[$name]['name']) {
       //买过的话,则总价格增加,相应商品数量增加
       $_SESSION['totalPrice'] += $price;
       $goods[$name]['number'] += 1;
   } else {
       //第一次买的话,将相应的商品信息添加到session中
       $goods[$name]['name'] = $name;
       $goods[$name]['price'] = $price;
       $goods[$name]['number'] += 1;
       $_SESSION['totalPrice'] += $price;
   }

   $_SESSION['goods'] = $goods;
   //购买处理完毕后跳转到商品列表
   header('location: goodsList.php');
?>
</body>
</html>

shoppingCart.php This page displays the products, prices, total price and other information in the shopping cart.

The renderings are as follows:

document_2015-09-04_55e945fc2e667.png

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<?php
session_start();
//将session中的商品信息(即购物车中的商品)和总价显示到页面
$goods = $_SESSION['goods'];
echo '您买了:<br />';
foreach ($goods as $value) {
   echo $value['name'] . ' 价格 ' . $value['price'] . ' 数量 ' . $value['number'] . '<br />';
}
echo '总价:' . $_SESSION['totalPrice'] . '<br />';

?>
<a href="goodsList.php">返回商品列表</a>
</body>
</html>

The shopping cart example is completed. Do you feel a sense of accomplishment after completing it yourself? ! you're good! !

Continuing Learning
||
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <?php session_start(); //将session中的商品信息(即购物车中的商品)和总价显示到页面 $goods = $_SESSION['goods']; echo '您买了:<br />'; foreach ($goods as $value) { echo $value['name'] . ' 价格 ' . $value['price'] . ' 数量 ' . $value['number'] . '<br />'; } echo '总价:' . $_SESSION['totalPrice'] . '<br />'; ?> <a href="goodsList.php">返回商品列表</a> </body> </html>
submitReset Code