Jump to content

Session variable


RynMan

Recommended Posts

Hey guys!

 

I'm trying to work through my shopping cart here and am having a problem.  Everytime I start up a new session (clear my cookies in firefox) and start testing, I get the following error....

 

Notice: Undefined variable: cart in C:\wamp\www\Adept\action_handle.inc.php  on line 16

 

It's pointing to my code here....

 

session_start();

$cart = $_SESSION['cart'];
$action = $_GET['action'];
$iquantity =  $_POST['iquantity'];

 

I'm guessing it's because the session has just started so naturally there will be nothing in the 'cart'.  How is this usually avoided?  Do we need to assign something to the cart to start with?  I'm trying not to do that, as my cart counts the number of instances a number (ID) appears....so I can't really have any values in there to start.

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/204576-session-variable/
Share on other sites

Do something like this:

<?php
session_start();

$cart = (isset($_SESSION['cart']))?$_SESSION['cart']:'';
$action = $_GET['action'];
$iquantity =  $_POST['iquantity'];
?>

 

This will assign a 0 character string to $cart if there is no 'cart' session variable. If there is one, $cart will be assigned it's value.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/204576-session-variable/#findComment-1071159
Share on other sites

Do something like this:

<?php
session_start();

$cart = (isset($_SESSION['cart']))?$_SESSION['cart']:'';
$action = $_GET['action'];
$iquantity =  $_POST['iquantity'];
?>

 

This will assign a 0 character string to $cart if there is no 'cart' session variable. If there is one, $cart will be assigned it's value.

 

Ken

 

Awesome!  Thanks Ken! :)

Link to comment
https://forums.phpfreaks.com/topic/204576-session-variable/#findComment-1071312
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.