本文介紹一個php實現的購物車程式碼,功能實現比較完整,實現了購物車、訂貨人資訊、會員價等功能,還是不錯的,有需要的朋友,可以參考下
1.商品展示頁面
<table width="255" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="130" rowspan="6"><p align="center"> <?php if(trim($info[tupian]=="")) { echo "暂无图片"; } else { ?> <img src="/static/imghwm/default1.png" data-src="<?php echo $info[tupian];? alt="關於php實現購物車的程式碼一" >" class="lazy" width="130" style="max-width:90%" border="0"> <?php } ?> </p></td> <td width="20" height="16"> </td> <td width="113"><font color="EF9C3E">【<?php echo $info[mingcheng];?>】</font></td> </tr> <tr> <td height="16"> </td> <td><font color="910800">【市场价:<?php echo $info[shichangjia];?>】</font></td> </tr> <tr> <td height="16"> </td> <td><font color="DD4679">【会员价:<?php echo $info[huiyuanjia];?>】</font></td> </tr> <tr> <td height="16"> </td> <td>【<a href="lookinfo.php?id=<?php echo $info[id];?>">查看信息</a>】</td> </tr> <tr> <td height="16"> </td> <td>【<a href="addgouwuche.php?id=<?php echo $info[id];?>">放入购物车</a>】</td> </tr> <tr> <td height="16"> </td> <td><font color="13589B">【剩余数量: <?php if(($info[shuliang]-$info[cishu])>0) { echo ($info[shuliang]-$info[cishu]); } else { echo "已售完"; } ?>】</font></td> </tr> </table> <?php } ?> </table>
2、檔案addgouwuche.php
<?php session_start(); include("conn.php"); if($_SESSION[username]=="") { echo "<script>alert('请先登录后购物!');history.back();</script>"; exit; } $id=strval($_GET[id]); $sql=mysql_query("select * from shangpin where id='".$id."'",$conn); $info=mysql_fetch_array($sql); if($info[shuliang]<=0) { echo "<script>alert('该商品已经售完!');history.back();</script>"; exit; } $array=explode("@",$_SESSION[producelist]); for($i=0;$i<count($array)-1;$i++) { if($array[$i]==$id) { echo "<script>alert('该商品已经在您的购物车中!');history.back();</script>"; exit; } } $_SESSION[producelist]=$_SESSION[producelist].$id."@"; $_SESSION[quatity]=$_SESSION[quatity]."1@"; header("location:gouwu1.php"); ?>
3、檔案gouwu1.php
<?php session_start(); if($_SESSION[username]=="") { echo "<script>alert('请先登录,后购物!');history.back();</script>"; exit; } ?> <?php include("top.php"); ?> <table width="800" height="438" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="200" height="438" valign="top" bgcolor="#E8E8E8"><p align="center"> <?php include("left.php");?> </p></td> <td width="10" background="images/line2.gif"> </td> <td width="590" valign="top"><table width="550" height="10" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td> </td> </tr> </table> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <form name="form1" method="post" action="gouwu1.php"> <tr> <td height="25" bgcolor="#555555"><p align="center" style="color: #FFFFFF"><?php echo $_SESSION[username];?>的购物车</p></td> </tr> <tr> <td bgcolor="#555555"><table width="500" border="0" align="center" cellpadding="0" cellspacing="1"> <?php session_start(); session_register("total"); if($_GET[qk]=="yes") { $_SESSION[producelist]=""; $_SESSION[quatity]=""; } $arraygwc=explode("@",$_SESSION[producelist]); $s=0; for($i=0;$i<count($arraygwc);$i++) { $s+=intval($arraygwc[$i]); } if($s==0 ) { echo "<tr>"; echo" <td height='25' colspan='6' bgcolor='#FFFFFF' align='center'>您的购物车为空!</td>"; echo"</tr>"; } else { ?> <tr> <td width="125" height="25" bgcolor="#FFFFFF"><p align="center">商品名称</p></td> <td width="52" bgcolor="#FFFFFF"><p align="center">数量</p></td> <td width="64" bgcolor="#FFFFFF"><p align="center">市场价</p></td> <td width="64" bgcolor="#FFFFFF"><p align="center">会员价</p></td> <td width="51" bgcolor="#FFFFFF"><p align="center">折扣</p></td> <td width="66" bgcolor="#FFFFFF"><p align="center">小计</p></td> <td width="71" bgcolor="#FFFFFF"><p align="center">操作</p></td> </tr> <?php /** * 购物车 商品数量管理 * Edit www.jb51.net */ $total=0; $array=explode("@",$_SESSION[producelist]); $arrayquatity=explode("@",$_SESSION[quatity]); while(list($name,$value)=each($_POST)) { for($i=0;$i<count($array)-1;$i++) { if(($array[$i])==$name) { $arrayquatity[$i]=$value; } } } $_SESSION[quatity]=implode("@",$arrayquatity); for($i=0;$i<count($array)-1;$i++) { $id=$array[$i]; $num=$arrayquatity[$i]; if($id!="") { $sql=mysql_query("select * from shangpin where id='".$id."'",$conn); $info=mysql_fetch_array($sql); $total1=$num*$info[huiyuanjia]; $total+=$total1; $_SESSION["total"]=$total; ?> <tr> <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[mingcheng];?></p></td> <td height="25" bgcolor="#FFFFFF"><p align="center"> <input type="text" name="<?php echo $info[id];?>" size="2" class="inputcss" value=<?php echo $num;?>> </p></td> <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[shichangjia];?>元</p></td> <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[huiyuanjia];?>元</p></td> <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo @(ceil(($info[huiyuanjia]/$info[shichangjia])*100))."%";?></p></td> <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[huiyuanjia]*$num."元";?></p></td> <td height="25" bgcolor="#FFFFFF"><p align="center"><a href="removegwc.php?id=<?php echo $info[id]?>">移除</a></p></td> </tr> <?php } } ?> <tr> <td height="25" colspan="8" bgcolor="#FFFFFF"><p align="right"> <table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="125"><p align="center"> <input type="submit" value="更改商品数量" class="buttoncss"> </p></td> <td width="125"><p align="center"><a href="gouwu2.php">去收银台</a></p></td> <td width="125"><p align="center"><a href="gouwu1.php?qk=yes">清空购物车</a></p></td> <td width="125"><p align="left">总计:<?php echo $total;?></p></td> </tr> </table> </p></td> </tr> <?php } ?> </table></td> </tr> </form> </table></td> </tr> </table>
3、文件gouwu2.php
<table width="800" height="438" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="200" height="438" valign="top" bgcolor="#E8E8E8"><p align="center"> <?php include("left.php");?> </p></td> <td width="10" background="images/line2.gif"> </td> <td width="590" valign="top"><table width="550" height="15" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td> </td> </tr> </table> <table width="550" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="25" bgcolor="#555555"><p align="center" style="color: #FFFFFF">收货人信息</p></td> </tr> <tr> <td height="300" bgcolor="#555555"><table width="550" height="300" border="0" align="center" cellpadding="0" cellspacing="1"> <script language="javascript"> /** * 购物车 收货人信息 * Edit www.jb51.net */ function chkinput(form) { if(form.name.value=="") { alert("请输入收货人姓名!"); form.name.select(); return(false); } if(form.dz.value=="") { alert("请输入收货人地址!"); form.dz.select(); return(false); } if(form.yb.value=="") { alert("请输入收货人邮编!"); form.yb.select(); return(false); } if(form.tel.value=="") { alert("请输入收货人联系电话!"); form.tel.select(); return(false); } if(form.email.value=="") { alert("请输入收货人E-mail地址!"); form.email.select(); return(false); } if(form.email.value.indexOf("@")<0) { alert("收货人E-mail地址格式输入错误!"); form.email.select(); return(false); } return(true); } </script> <form name="form1" method="post" action="savedd.php" onSubmit="return chkinput(this)"> <tr> <td width="100" height="25" bgcolor="#FFFFFF"><p align="center">收货人姓名:</p></td> <td width="183" bgcolor="#FFFFFF"><p align="left"><input type="text" name="name" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></p></td> <td width="86" bgcolor="#FFFFFF"><p align="center">性别:</p></td> <td width="176" bgcolor="#FFFFFF"><p align="left"> <select name="***"> <option selected value="男">男</option> <option value="女">女</option> </select> </p></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><p align="center">详细地址:</p></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input name="dz" type="text" class="inputcss" id="dz" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'" size="25"> </p></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><p align="center">邮政编码:</p></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input type="text" name="yb" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></p></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><p align="center">联系电话:</p></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input type="text" name="tel" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></p></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><p align="center">电子邮箱:</p></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input type="text" name="email" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></p></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><p align="center">送货方式:</p></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"> <select name="shff" id="shff"> <option selected value="普通平邮">普通平邮</option> <option value="特快专递">特快专递</option> <option value="送货上门">送货上门</option> <option value="个人送货">个人送货</option> <option value="E-mail">E-mail</option> </select> </p></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><p align="center">支付方式:</p></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"> <select name="zfff" id="zfff"> <option selected value="建设银行汇款">建设银行汇款</option> <option value="交通银行汇款">交通银行汇款</option> <option value="邮局汇款">邮局汇款</option> <option value="网上支付">网上支付</option> </select> </p></td> </tr> <tr> <td height="100" bgcolor="#FFFFFF"><p align="center">简单留言:</p></td> <td height="100" colspan="3" bgcolor="#FFFFFF"><p align="left"> <textarea name="ly" cols="60" rows="8" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></textarea> </p></td> </tr> <tr> <td height="25" colspan="4" bgcolor="#FFFFFF"><p align="center"><input type="submit" value="提交订单" class="buttoncss"> </p></td> </tr> </form> </table></td> </tr> </table></td> </tr> </table> <?php if($_GET[dingdanhao]!="") { $dd=$_GET[dingdanhao]; session_start(); $array=explode("@",$_SESSION[producelist]); $sum=count($array)*20+260; echo" <script language='javascript'>"; echo" window.open('showdd.php?dd='+'".$dd."','newframe','top=150,left=200,width=600,height=".$sum.",menubar=no,toolbar=no,location=no,scrollbars=no,status=no ')"; echo "</script>"; } ?>
4、資料庫設定檔conn.php
<?php $conn=mysql_connect("localhost","root","") or die("数据库服务器连接错误".mysql_error()); mysql_select_db("shop",$conn) or die("数据库访问错误".mysql_error()); mysql_query("set character set gb2312"); mysql_query("set names gb2312"); ?>
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
#
以上是關於php實現購物車的程式碼一的詳細內容。更多資訊請關注PHP中文網其他相關文章!

防止會話固定攻擊的有效方法包括:1.在用戶登錄後重新生成會話ID;2.使用安全的會話ID生成算法;3.實施會話超時機制;4.使用HTTPS加密會話數據,這些措施能確保應用在面對會話固定攻擊時堅不可摧。

實現無會話身份驗證可以通過使用JSONWebTokens(JWT)來實現,這是一種基於令牌的認證系統,所有的必要信息都存儲在令牌中,無需服務器端會話存儲。 1)使用JWT生成和驗證令牌,2)確保使用HTTPS防止令牌被截獲,3)在客戶端安全存儲令牌,4)在服務器端驗證令牌以防篡改,5)實現令牌撤銷機制,如使用短期訪問令牌和長期刷新令牌。

PHP會話的安全風險主要包括會話劫持、會話固定、會話預測和會話中毒。 1.會話劫持可以通過使用HTTPS和保護cookie來防範。 2.會話固定可以通過在用戶登錄前重新生成會話ID來避免。 3.會話預測需要確保會話ID的隨機性和不可預測性。 4.會話中毒可以通過對會話數據進行驗證和過濾來預防。

銷毀PHP會話需要先啟動會話,然後清除數據並銷毀會話文件。 1.使用session_start()啟動會話。 2.用session_unset()清除會話數據。 3.最後用session_destroy()銷毀會話文件,確保數據安全和資源釋放。

如何改變PHP的默認會話保存路徑?可以通過以下步驟實現:在PHP腳本中使用session_save_path('/var/www/sessions');session_start();設置會話保存路徑。在php.ini文件中設置session.save_path="/var/www/sessions"來全局改變會話保存路徑。使用Memcached或Redis存儲會話數據,如ini_set('session.save_handler','memcached');ini_set(

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然後使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

在PHP會話中可以存儲數組。 1.啟動會話,使用session_start()。 2.創建數組並存儲在$_SESSION中。 3.通過$_SESSION檢索數組。 4.優化會話數據以提升性能。

PHP會話垃圾回收通過概率機制觸發,清理過期會話數據。 1)配置文件中設置觸發概率和會話生命週期;2)可使用cron任務優化高負載應用;3)需平衡垃圾回收頻率與性能,避免數據丟失。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

禪工作室 13.0.1
強大的PHP整合開發環境