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:
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:
<?php session_start(); if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) { $userName = $_POST['username']; $password = $_POST['password']; //從db獲取用戶信息 數(shù)據(jù)庫(kù)信息改成自己的 $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) { //密碼驗(yàn)證通過(guò),設(shè)置session,把用戶名和密碼保存在服務(wù)端 $_SESSION['username'] = $username; $_SESSION['password'] = $password; //最后跳轉(zhuǎn)到登錄后的歡迎頁(yè)面 //注意:這里我們沒(méi)有像cookie一樣帶參數(shù)過(guò)去 header('Location: welcome.php'); } } ?> <html> <head> <!-- 這里指明頁(yè)面編碼 --> <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:
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(); //從數(shù)據(jù)庫(kù)獲取商品信息存入$goods二維數(shù)組 $i = 0; //這里請(qǐng)換上自己的數(shù)據(jù)庫(kù)相關(guān)信息 $conn = mysqli_connect('host','username','password','test'); $res = mysqli_query($conn,'select * from shop'); //這里把商品信息放到$goods二維數(shù)組,每一維存的是單個(gè) //商品的信息,比如商品名、價(jià)格。 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 //取出商品信息顯示在頁(yè)面上,并添加購(gòu)買(mǎi)功能 foreach ($goods as $value) { echo ' 商品名 ' . $value['name'] . ' 價(jià)格 ' . $value['price']; echo "<a href=buy.php?name=" . $value['name'] . '&price=' . $value['price'] .">購(gòu)買(mǎi)</a>"; echo '<br />'; } ?> <a href="shoppingCart.php">查看購(gòu)物車</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 //開(kāi)啟session session_start(); //獲取傳過(guò)來(lái)的商品名和價(jià)格 $name = $_GET['name']; $price = $_GET['price']; //把session中的商品信息和傳過(guò)來(lái)的(剛買(mǎi)的)商品信息對(duì)比 $goods = $_SESSION['goods']; if ($name == $goods[$name]['name']) { //買(mǎi)過(guò)的話,則總價(jià)格增加,相應(yīng)商品數(shù)量增加 $_SESSION['totalPrice'] += $price; $goods[$name]['number'] += 1; } else { //第一次買(mǎi)的話,將相應(yīng)的商品信息添加到session中 $goods[$name]['name'] = $name; $goods[$name]['price'] = $price; $goods[$name]['number'] += 1; $_SESSION['totalPrice'] += $price; } $_SESSION['goods'] = $goods; //購(gòu)買(mǎi)處理完畢后跳轉(zhuǎn)到商品列表 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:
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <?php session_start(); //將session中的商品信息(即購(gòu)物車中的商品)和總價(jià)顯示到頁(yè)面 $goods = $_SESSION['goods']; echo '您買(mǎi)了:<br />'; foreach ($goods as $value) { echo $value['name'] . ' 價(jià)格 ' . $value['price'] . ' 數(shù)量 ' . $value['number'] . '<br />'; } echo '總價(jià):' . $_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! !