Forum Moderators: coopster

Message Too Old, No Replies

PHP Sessions

         

aanton05

5:55 pm on Dec 6, 2005 (gmt 0)

10+ Year Member



Hello. I have a shopping cart class. In my shopping cart class i have a cart_add Function. At the start of this function is the following code:

$cart_id = $this->get_cart_id();
if(!$cart_id){
// If no cart id found generate one
$unique_cid = md5(uniqid(rand (),1));
// Set cart id into the cookie
setcookie('cid',$unique_cid,time()+24*3600*60);

// Register session with cart id value
$_SESSION['cid'] = $_COOKIE['cid'];

// If person is a member
// modify their profile with
// cart id in the database

if($_SESSION['login']){
$_SESSION['cid'] = $_COOKIE['cid'];
@mysql_query("UPDATE members SET cart_id = '{$_SESSION['cid']}' WHERE id = '".$_SESSION['userid']."'");
}
}

The code above checks to see if a cart ID exists. If a unique cart ID does not exist, it will generate one and assign it $_SESSION['cid'].

Later in the cart_add Function it will utilize $_SESSION['cid'] in the following code:


if($product_qty>0){
$sql = mysql_query("INSERT INTO shopping_carts(cart_identifier,product_id,product_title,product_qty,product_price,date) VALUES('{$_SESSION['cid']}','$product_id','{$products['product_title']}','$product_qty','{$products['product_price']}',NOW())");
}else{
$sql = FALSE;
}

The problem is that the $_SESSION['cid'] value is not getting assigned to the cart_identifier field of the shopping_carts table in my database the first time(after $_SESSION['cid'] gets assigned the cart ID). However, the second time and thereafter that the cart_add Function gets called, the $_SESSION['cid'] does gets assigned to the cart_identifier field of the shopping_carts table. Does anybody know why this is happening? Thanks.

StupidScript

9:07 pm on Dec 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There doesn't seem to be any problem with your Session stuff, it's in the MySQL stuff that the breakdown occurs ... all because of the lowly apostrophe ...

@mysql_query("UPDATE members SET cart_id = [b]'{$_SESSION['cid']}'[/b] WHERE id = '".$_SESSION['userid']."'");

The problem is that you are using apostrophes within apostrophes for both queries, and that's breaking the query. You might want to assign a new variable to hold the values you want to use in the queries:

$curr_cid="{".$_SESSION['cid']."}";

@mysql_query("UPDATE members SET cart_id = '$curr_cid' WHERE id = '".$_SESSION['userid']."'");

Similarly (although not a problem with the queries):

$curr_cid="{".$_SESSION['cid']."}";

$curr_uid=$_SESSION['userid'];

@mysql_query("UPDATE members SET cart_id = '$curr_cid' WHERE id = '$curr_uid'");

It's a little amazing that the INSERT query works at all, given the nested apostrophes therein.

Hope that helps!

thebigstar

9:51 pm on Dec 6, 2005 (gmt 0)

10+ Year Member



with sessions how do you set it soo the cookie never times out untill they log out

ive tried many differnt ways and they dont seem to work

aanton05

12:23 am on Dec 7, 2005 (gmt 0)

10+ Year Member



Hello and Thanks. The MYSQL query is not the problem. It works fine. The problem is that the first time i try to assign the value of $_SESSION['cid'] after registering it($_SESSION['cid'] = $_COOKIE['cid'];) to cart_id in the MYSQL query, cart_id doesn't get assigned the value of $_SESSION['cid']. When i look at the database via phpMyAdmin, i can see that the new row is there. However, the cart_id field is empty while the rest of the field has been assigned. But the next time my cart_add Function is called and the same MYSQL query is utilized, it works fine(cart_id field gets assigned the value of $_SESSION['cid'].

So, what i am saying is that when i try to assign the value of $_SESSION['cid'] to something the first time after registering it, that variable that i am trying to assign it to is not getting assigned the value of $_SESSION['cid']. However, the next i try and assign the value of $_SESSION['cid'] to something, it works.

FalseDawn

3:42 am on Dec 7, 2005 (gmt 0)

10+ Year Member



I haven't fully analyzed your code, but the problem might be that you cannot set and then retrieve a cookie value in the same script (i.e. same HTTP request)

aanton05

4:48 am on Dec 7, 2005 (gmt 0)

10+ Year Member



Thanks. That's what i thought it was but i wanted to make sure.