yandoo Posted June 26, 2008 Share Posted June 26, 2008 Hi there, ive got a bit of a problem, infact its quite a tickler and i was hoping for a little help. I followed a tutorial to add a simple shopping cart to my website from: http://www.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart The shopping cart allows a user through a session to add, modify and delete items from my shop. My problem is that I want to add a login function to my website too but it doesnt seem to work...I thinking its because i use a session to login a user also. I created a test page that Simple said "Welcome" and presents the username of the logged in user. But nothing appears? Further more, i created a simple test conditional to display something based on the users access level, still no good?! I know its a bit vauge but if anybody could help me that would be so cool. heres my test page: <?php require_once('Connections/RFW.php'); ?> <?php $colname_user_conditional = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_user_conditional = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_RFW, $RFW); $query_user_conditional = sprintf("SELECT * FROM customer WHERE Username = '%s'", $colname_user_conditional); $user_conditional = mysql_query($query_user_conditional, $RFW) or die(mysql_error()); $row_user_conditional = mysql_fetch_assoc($user_conditional); $totalRows_user_conditional = mysql_num_rows($user_conditional); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <p>welcome <?php echo $row_user_conditional['Username']; ?></p> <p> <?php // Show IF Conditional region1 //echo @$row_user['Username']; if (@$row_user_conditional['Access'] == "2") { echo "Admin: </p>"; } ?> <p> </p> </body> </html> <?php mysql_free_result($user_conditional); ?> heres the cart code: <?php // Include MySQL class require_once('inc/mysql.class.php'); // Include database connection require_once('inc/global.inc.php'); // Include functions require_once('inc/functions.inc.php'); // Start the session session_start(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['ProductID']; } else { $cart = $_GET['ProductID']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['ProductID'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Shopping cart RoninFightWear · Cart</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1 align="center">Your Shopping Cart</h1> <div align="center"> <?php echo writeShoppingCart(); ?> </div> </div> <div id="contents"> <h1 align="center">Please check quantities...</h1> <div align="center"> <?php echo showCart(); ?> </div> <p align="center"><a href="products.php">Continue shopping with RoninFightWear</a></p> </div> <p align="center"><a href="buy.php" target="_self">Buy It Now</a><a href="login.php"></a></p> <p align="center"><img src="Images/111.jpg" alt="" width="120" height="119" /></p> </body> </html> And heres the Session used in the function of the cart: <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You currently have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have '.count($items).' item'.$s.' in your shopping cart</a></p>'; } } function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM products WHERE ProductID = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td>'.$ProductName.' by '.$Description.'</td>'; $output[] = '<td>£'.$Cost.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($Cost * $qty).'</td>'; $total += $Cost * $qty; $output[] = '<td><a href="cart.php?action=delete&ProductID='.$id.'" class="r">Remove item</a></td>'; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } ?> Thanks for listening Quote Link to comment Share on other sites More sharing options...
simon551 Posted June 26, 2008 Share Posted June 26, 2008 do you have a session_start(); at the beginning of the page? Quote Link to comment Share on other sites More sharing options...
yandoo Posted June 26, 2008 Author Share Posted June 26, 2008 Hi there, Thats great that did clear up that little bug, thank you. Although I was wondering if i could some how i could integrate the cart session with the login user session? Because at the moment a new user can log in only to find they have items in a cart from a previous user. Is that possible?? Thank You Quote Link to comment Share on other sites More sharing options...
simon551 Posted June 26, 2008 Share Posted June 26, 2008 a new user is using the same machine without closing the browser? if so you could clear out the cart session. $_SESSION['cart']=""; Quote Link to comment Share on other sites More sharing options...
GreenUser Posted June 26, 2008 Share Posted June 26, 2008 session_unregister("LOGGEDIN"); session_unregister("SOMETHING"); session_unregister("SOMETHINGELSE"); Something like that in there should do the trick,as well. Quote Link to comment Share on other sites More sharing options...
yandoo Posted June 26, 2008 Author Share Posted June 26, 2008 Hi, Thanks for reply, yes thats it, im quite new to this, but where would add the $_SESSION['cart']=""; code to achieve this please?? Thank You Quote Link to comment Share on other sites More sharing options...
GreenUser Posted June 26, 2008 Share Posted June 26, 2008 Would a log out page work for you? They must be logged in to order, is that correct? Quote Link to comment Share on other sites More sharing options...
Stooney Posted June 26, 2008 Share Posted June 26, 2008 you could add a 'user_last_action' variable to your sessions. Each time they load a page check that value against the current time and if the user was inactive for say 10 minutes, destroy the sessions and make them log back in. If the user was not inactive long enough, leave them logged in and just update the session value to the current time. That is usually done with a database rather than sessions, but it still works. Quote Link to comment Share on other sites More sharing options...
simon551 Posted June 26, 2008 Share Posted June 26, 2008 but where would add the $_SESSION['cart']=""; code to achieve this please?? right after login- wherever that is. Quote Link to comment Share on other sites More sharing options...
yandoo Posted June 26, 2008 Author Share Posted June 26, 2008 Cool, Thank You Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.