I am building a shopping cart with php, and i have all the items able to pass through to the cart page. 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?
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. I have read the php manual page, but still am at a loss…my data doesnt transfer. 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. 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$_SESSIONarray as you would any other array. Look at thesession_start()page for an example.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.
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.