Sessions are slowly killing me

So I have this script which displays multiple user’s stores, so it needs to create a uniquely identifiable cart session so that the user can visit several different stores without their cart being duplicated on someone else’s store…

I’m just at loose ends with this and it seems to recreate the same basket on different stores!

Any ideas? Greatly appreciated!

<?php
session_start();
include("dbstore.php"); 

$storeurl = addslashes($_GET['url']);

$getstore = mysql_query("SELECT * FROM `store` WHERE `url` = '$storeurl'") or die(mysql_error()); 
$store = mysql_fetch_array($getstore);
$current = time(); //Current timestamp 


// Grab shopping cart session if it exists

if(isset($_SESSION["cart_id_set_$store[id]"])) {

$getbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '".$_SESSION['cart_id_$store[id]']."'") or die(mysql_error()); 
$basket = mysql_fetch_assoc($getbasket);

$get_basket_items = mysql_query("SELECT * FROM `store_basket_items` WHERE `basketid` = '$basket[id]'") or die(mysql_error());
$total_items = mysql_num_rows($get_basket_items);

$basket_total = mysql_query("SELECT SUM(price) AS price FROM store_basket_items WHERE basketid = '$basket[id]'");
$total = mysql_fetch_array($basket_total);

$total_price_format = number_format($total[price], 2, '.', ',');

echo"<div id=\\"basket\\">$total_items Items - £$total_price_format   <u>Check Out</u></div>";




// Create shopping cart session if it does not exist yet..

} else {

$current2 = time(); //Current timestamp 
$countcarts = mysql_query("SELECT MAX(id) AS maximumId FROM `store_basket") or die(mysql_error()); 
$totalCarts = mysql_fetch_object($countcarts); 
$new_totalCarts = ($totalCarts->maximumId+1); 

$createcart = mysql_query("INSERT INTO `store_basket` (`id`, `timestamp`) VALUES('" . mysql_real_escape_string($new_totalCarts) . "', '" . $current2 . "')") or die(mysql_error()); 
$basketid = mysql_insert_id(); 


$_SESSION["cart_id_$store[id]"] = $basketid; 
$_SESSION["cart_id_set_$store[id]"] = 'yes'; 

echo"<div id=\\"basket\\">0 Items - £0.00   <u>Check Out</u></div>";

}


?>

Thanks for suggestions once again but I just cannot get a hang of this, any ideas?

<?php
session_start();
include("dbstore.php"); 

$storeurl = addslashes($_GET['url']);

$getstore = mysql_query("SELECT * FROM `store` WHERE `url` = '$storeurl'") or die(mysql_error()); 
$store = mysql_fetch_array($getstore);
$storeId = $store['id'];

$current = time(); //Current timestamp 


// Grab shopping cart session if it exists

if(isset($_SESSION['store'][$storeId]['cart'])) {

$sessionvalue = $_SESSION['store'][$storeId]['cart'];

$getbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '$sessionvalue'") or die(mysql_error()); 
$basket = mysql_fetch_assoc($getbasket);

$get_basket_items = mysql_query("SELECT * FROM `store_basket_items` WHERE `basketid` = '$basket[id]'") or die(mysql_error());
$total_items = mysql_num_rows($get_basket_items);

$basket_total = mysql_query("SELECT SUM(price) AS price FROM store_basket_items WHERE basketid = '$basket[id]'");
$total = mysql_fetch_array($basket_total);

$total_price_format = number_format($total[price], 2, '.', ',');

echo"<div id=\\"basket\\">$total_items Items - £$total_price_format   <u>Check Out</u></div>";




// Create shopping cart session if it does not exist yet..

} else {

$current2 = time(); //Current timestamp 
$countcarts = mysql_query("SELECT MAX(id) AS maximumId FROM `store_basket") or die(mysql_error()); 
$totalCarts = mysql_fetch_object($countcarts); 
$new_totalCarts = ($totalCarts->maximumId+1); 

$createcart = mysql_query("INSERT INTO `store_basket` (`id`, `timestamp`) VALUES('" . mysql_real_escape_string($new_totalCarts) . "', '" . $current2 . "')") or die(mysql_error()); 
$basketid = mysql_insert_id(); 


$_SESSION['store'][$storeId]['cart'] = $new_totalCarts; 
$_SESSION["cart_id_set_$store[id]"] = 'yes'; 

echo"<div id=\\"basket\\">0 Items - £0.00   <u>Check Out</u>not set yet</div>";

}


?>

I’m now trying this code out, but it results in displaying BOTH print_r’s as if it thinks it is set and isn’t, and the basket entries into SQL aren’t even turning up!

So confusing!!


<?php
session_start();
include("dbstore.php"); 

$storeurl = addslashes($_GET['url']);

$getstore = mysql_query("SELECT * FROM `store` WHERE `url` = '$storeurl'") or die(mysql_error()); 
$store = mysql_fetch_array($getstore);
$storeId = $store['id'];

$current = time(); //Current timestamp 


// Grab shopping cart session if it exists
if(!isset($_SESSION['store'][$storeId]['cart'])) {

$current2 = time(); //Current timestamp 
$countcarts = mysql_query("SELECT MAX(id) AS maximumId FROM `store_basket") or die(mysql_error()); 
$totalCarts = mysql_fetch_object($countcarts); 
$new_totalCarts = ($totalCarts->maximumId+1); 

$createcart = mysql_query("INSERT INTO `store_basket` (`id`, `timestamp`) VALUES('" . mysql_real_escape_string($new_totalCarts) . "', '" . $current2 . "')") or die(mysql_error()); 
$basketid = mysql_insert_id(); 

$_SESSION['store'][$storeId]['cart'] = $new_totalCarts;

print_r($_SESSION['store'][$storeId]['cart']);

}



$sessionvalue = $_SESSION['store'][$storeId]['cart'];

$getbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '$sessionvalue'") or die(mysql_error()); 
$basket = mysql_fetch_assoc($getbasket); 

$get_basket_items = mysql_query("SELECT * FROM `store_basket_items` WHERE `basketid` = '$basket[id]'") or die(mysql_error());
$total_items = mysql_num_rows($get_basket_items);

$basket_total = mysql_query("SELECT SUM(price) AS price FROM store_basket_items WHERE basketid = '$basket[id]'");
$total = mysql_fetch_array($basket_total);

$total_price_format = number_format($total[price], 2, '.', ',');

print_r($_SESSION['store'][$storeId]['cart']);

echo"<div id=\\"basket\\">$total_items Items - £$total_price_format   <u>Check Out</u></div>";


?>

Thanks for all your continued help

The problem seems to be that it doesn’t set the session (that or be able to read it), as it defaults to the else statement and displays “not yet set” which I told it to within the echo on the else statement :frowning:

Thanks for your help

 $_SESSION["cart_id_set_$store[id]"] 

Mainly your problem is… $store[id] is not going to be parsed in your session variable correctly with the way you are doing it. Either rename like this:

 $_SESSION['cart_id_set_' . $store['id']] 

Or choose a nicer looking session scheme…


...
$store = mysql_fetch_array($getstore);
$storeId = $store['id'];

$_SESSION['store'][$storeId]['cart'];

Also… if each store is going to have a unique URL, as long as you clean that URL variable, i would use that instead of the store id… since that is a unique identifier.


// Rough
$storeUrl = htmlentities($_GET['url']);

// WOuld probably want to check to see if it is a valid url.. 
$_SESSION['store'][$storeUrl]['cart'];

Yes, store.php?url= (subdomain selected by user)

Would that cause a big problem?

oh… all the stores under the same domain?

What’s wrong? Also, in your queries don’t do $basket[id] it is better to do:


" ...  WHERE `basketid` = " . $basket['id']) or die(...

You should echo your variables and print_r() your arrays to see if the values you think you are getting are showing up.

You just said your problem… :stuck_out_tongue: It doesn’t set the session. Why is that?


if(isset($_SESSION['store'][$storeId]['cart'])) {

You check if the session is set… it’s not… so you probably should declare a line above it to set it.


if(!isset($_SESSION['store'][$storeId]['cart'])) {
     $_SESSION['store'][$storeId] = array(
        'cart' => array(),
    );
}

I don’t know how your code is entirely supposed to work, but you should be able to figure it out now.