Jump to content

Session params and shopping cart


jason360

Recommended Posts

Hey guys,

 

I have a website that is using a login.  I want add a shopping cart to the site in a separate folder (mysite.com/cart).  I want the user to be able to say signed in for a couple weeks without having to re login so I am using:

 

session_set_cookie_params(2*7*24*60*60, '/');

 

I am using sessions for my cart.  The existing settings works fine with cart however the cart is holding the items for the length of the two week cookie params, and I want the cart to be reset when the window is closed, but still logged in.

 

I have never used sessions before and logins.  Is it possible to use different params or should i be using some kind of unset function?  I am really not sure the best way to go about this.  I don't want the user to have to login separately when they use the cart.  Any ideas on the best way to do this would be appreciated.  Please note i am a bit of a programming noob.

 

Thanks,

 

JK

 

My cart script: mysite.com/cart/cart.php


include("../config.inc.php");
session_name('wkLogin');
session_set_cookie_params(0);
session_start();



if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']).'');
while ($quantity_row = mysql_fetch_assoc($quantity)){
	if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
		$_SESSION['cart_'.(int)$_GET['add']]+='1';
	}
}
header('location: http://www.mysite.com/cart/view.php?pid='.$_GET['add'].'');	
}


function cart() {
foreach($_SESSION as $name => $value) {
	if ($value>0) {
		if (substr($name, 0 , 5) =='cart_') {
			$id = substr($name, 5, (strlen($name) -5));
			$get = mysql_query ('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
			while ($get_row = mysql_fetch_assoc($get)) {
				$sub = $get_row['price']*$value;

			}

		}
		$total += $sub;

	}

}
if ($total==0) {
	echo "Your cart is empty.";
}
else {
	echo '<p>Total: &#36;'.number_format($total, 2).'</p>';

        
}
}




 

 

 

 

 

My cart view item page: mysite.com/cart/view.php?pid=2

 


define('INCLUDE_CHECK',true);

require('../config.inc.php');
require('../functions.php');
require('../cart/cart.php');




require '../login.php';


// initialization

$result_array = array();

$counter = 0;

$pid = ($_GET['pid']);


// Full Size View of Photo

if( $pid )

{

	$result = mysql_query( "SELECT * FROM products WHERE id='".addslashes($pid)."'" );

	list($id, $name, $description, $snippet, $link, $price, $shipping, $quantity, $anchor) = mysql_fetch_array( $result );

	$nr = mysql_num_rows( $result );

	mysql_free_result( $result );	


}


.......







 

 

My login page: mysite.com/index.php

 

define('INCLUDE_CHECK',true);

require "connect.php";
require 'functions.php';
// Those two files can be included only if INCLUDE_CHECK is defined


session_name('wkLogin');
// Starting the session

session_set_cookie_params(2*7*24*60*60, '/');
// Making the cookie live for 2 weeks

session_start();

require 'login.php';

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/265137-session-params-and-shopping-cart/
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.