Jump to content

Add to cart only if user is logged in??


mikebyrne

Recommended Posts

At present when the user clicks add to cart the product automatically goes into the shopping cart. Is there a way I can chack to see if the user has logged on before the varibles are passed onto the cart??

 

For example each page would have a session start() and a typical "ADD TO CART" button would be codded like this:

 

<td align="center"><a href="cart.php?action=add&id=17"><img src="http://images-cache.cd-wow.com/images/testdev/cdw_tdev_atc_v1_red.gif" class="cartimg" border="0" title="COLDPLAY - Viva La Vida - Add to cart!" alt="COLDPLAY - Viva La Vida - Add to cart!" height="16" width="60"></a>

 

 

My shopping cart looks like:

 

<?php
// Include MySQL class
require_once('mysql.class.php');
// Include database connection
require_once('global.inc.php');
// Include functions
require_once('functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['id'] != $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>PHP Shopping Cart Demo &#0183; Cart</title>
<link rel="stylesheet" href="styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

<h1>Please check quantities...</h1>

<?php
echo showCart();
?>

<p><a href="login.php">Back to shop...</a></p>

</div>

</body>
</html>

 

 

Link to comment
Share on other sites

Im assuming uve created one or more session variables upon loggin in. Try the following

 

if (isset($_SESSION['elementname'])) {

  // add to cart

}

else {

  // redirect user to login page

}

 

Check to see if the session variable is set, if not use 'header' to redirect the user to the login page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.