[英]How can I keep a shopping cart populated when the user navigates away from the 'cart' page, wihtout using a database
I am building a shopping cart with php, and i have all the items able to pass through to the cart page. 我正在用php构建购物车,并且我所有的物品都可以传递到购物车页面。 However, if the user were to navigate away from the page, all the items disappear.
但是,如果用户要离开页面导航,则所有项目都会消失。 I know that I can use to begin a session, but how do i persist data in the session without using a database?
我知道可以用来开始会话,但是如何在不使用数据库的情况下将数据持久化?
Should I learn HTML5 session-storage? 我应该学习HTML5会话存储吗?
Thanks, Eric. 谢谢,埃里克。 I agree, 'if it aint broke then dont try to fix it'...so, how exactly do I read/ write data to the $_SESSION array.
我同意,“如果它还没有损坏,请不要尝试对其进行修复”。。。因此,我如何准确地将数据读/写到$ _SESSION数组。 I have read the php manual page, but still am at a loss...my data doesnt transfer.
我已经阅读了php手册页,但仍然不知所措...我的数据没有传输。 Here's my code:
这是我的代码:
<?php session_start();
$mytext = $_SESSION['mytext'];
$mytext1 = $_SESSION['mytext1'];
$mytext2 = $_SESSION['mytext2'];
$mytext3 = $_SESSION['mytext3'];
$price = $_SESSION['price'];
$price1 = $_SESSION['price1'];
$price2 = $_SESSION['price2'];
$price3 = $_SESSION['price3'];
$total = $mytext * $price;
$total1 = $mytext1 * $price1;
$total2 = $mytext2 * $price2;
$total3 = $mytext3 * $price3;
?>' ?>”
By default, the PHP $_SESSION variable doesn't use a proper database or need you to install anything in addition to PHP. 默认情况下,PHP $ _SESSION变量不使用适当的数据库,或者除PHP外,您不需要安装其他任何东西。 It will simply store each users session data in a file on your server.
它将仅将每个用户的会话数据存储在服务器上的文件中。
After calling session_start()
you can simply read/write data to the $_SESSION
array as you would any other array. 调用
session_start()
您可以像其他数组一样简单地将数据读/写到$_SESSION
数组。 Look at the session_start()
page for an example. 查看
session_start()
页面中的示例。
No, I wouldn't do this in HTML 5. PHP sessions have been working well for years and won't have any browser compatibility issues. 不,我不会在HTML 5中执行此操作。PHP会话多年来一直运行良好,不会出现任何浏览器兼容性问题。
EDIT 编辑
Are you writing to the array first? 您是先写数组吗? If you haven't put the items in, you won't get anything out.
如果您没有放入物品,您将一无所获。 You'll notice that on the example in the linked page, there are two pages in play.
您会注意到,在链接页面的示例中,有两个页面在播放。 The first page assigns to the session array.
第一页分配给会话数组。 In your code, you only try to take things out of the session array.
在代码中,您仅尝试将内容从会话数组中取出。 Until you add items to it, you won't get anything out of it.
在向其中添加项目之前,您将一无所获。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.