Help with session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DudeBori82
Forum Commoner
Posts: 26
Joined: Thu Nov 18, 2004 10:09 am
Location: Florida

Help with session variables

Post by DudeBori82 »

I am building a shopping cart and have the following variables from the add to cart page:

$Product_ID_cart = $_POST['Product_ID'];
$short_desc = $_POST['short_desc'];
$style_ID = $_POST['style_color'];
$price_ID = $_POST['qty_rice'];
$parts = $_POST['parts'];

I need to insert them into a session array (which will act as a cart) and I am having trouble with the syntax of creating a session array and incrementing the values so that I can add, edit, and delete products. please help me find appropriate code snippets, or give me an example that can be applied.

Thank you! :)
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Code: Select all

session_start();
$_SESSION["cart"] = Array( /* your array contents here */);
With exactly which syntax you're having problems?
DudeBori82
Forum Commoner
Posts: 26
Joined: Thu Nov 18, 2004 10:09 am
Location: Florida

Post by DudeBori82 »

Well, I suppose it's more of a logistics error, I need to know if i should do a multi dimensional array because each item in the cart has multiple variables that need to be displayed. Let me know what you think is best. I also need help on the sintax of adding those previously listed variables into the session array.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

You can use $_SESSION array as you would use any other variable. Only difference is that it will remain available on other pages too.
You can assign it as you would assign any other array:

Code: Select all

session_start();
$_SESSION["cart"] = Array(
  "Product_ID" => $_POST['Product_ID'],
  "short_desc" => $_POST['short_desc'],
  "style_ID"   => $_POST['style_color'],
  "price_ID"   => $_POST['qty_rice'],
  "parts"      => $_POST['parts']
);
You can check the contents of your $_SESSION array like this:

Code: Select all

echo "<pre>";
Print_R($_SESSION);
echo "</pre>";
DudeBori82
Forum Commoner
Posts: 26
Joined: Thu Nov 18, 2004 10:09 am
Location: Florida

Post by DudeBori82 »

How do I destroy a sesson array (what sintax do I use?) Do session variables destroy themselved after a period of time, or do I have to write code to destroy them after a certain period of time?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

<?php

unset($_SESSION['cart']);

// or

unset($_SESSION['cart']['product1']);

// or kill the whole session

session_destroy();

?>

sessions die automatically when the browser stops sending the cookie, which is by default after the browser is closed.

take a look at http://php.net/session
theres tons of info about this kinda stuff


if you need a manual type expire for some reason

Code: Select all

$expire_secs = 3600; // 1 hr

session_start();

if (empty($_SESSION['last_activity'])) {
    $_SESSION['last_activity'] = time();
}


if ((time() - $_SESSION['last_activity']) > $expire_secs) {
     session_destroy();
     exit ('inactivity timeout');
} else {
     $_SESSION['last_activity'] = time();
}
DudeBori82
Forum Commoner
Posts: 26
Joined: Thu Nov 18, 2004 10:09 am
Location: Florida

Post by DudeBori82 »

Thanks!!!!
Post Reply