Page 1 of 1
Problem converting $_SESSION script to $HTTP_SESSION_VARS
Posted: Thu Nov 28, 2002 6:22 am
by dmorris
I have a script which works fantastically on my local machine with the new php. Moving it to my host - with an older version of php

has caused me all sorts of problems.
Having read the session ref, it says that I can
You can substitute $HTTP_SESSION_VARS for $_SESSION, if you prefer the former.
which I have done, but it still breaks. I have a horrible feeling that it is something to do with globals, but having never used them are unsure of how to solve that.
the snippet of code that breaks first is
Code: Select all
<?php
function recalculate(){
//This is used to update the variables holding the number of items and the price.
//It should decrease the number of database queries since get_total will not need
//to do any connections
$HTTP_SESSION_VARSї'total'] = 0.0;
foreach ($HTTP_SESSION_VARSї'products'] as $prod){
$query = "Select price from ProductCategory pc, Product p where p.pcid = pc.pcid and p.productID = '".$prodї'pid']."'";
if (!$this->db->query($query)){
die($this->db->error());
}
//retrieve the price and add it on to the total
$row = $this->db->fetchAssoc();
$HTTP_SESSION_VARSї'total'] += ($rowї'price'] * $prodї'quant']);
}
}//recalculate
?>
The way I have written the script is as a class, but with all class variables as session variables, so (my theory was) all functions have direct access to them. I can post the rest of the script if it will help
Any help is greatly appreciated
Duncan
Posted: Thu Nov 28, 2002 6:30 am
by twigletmac
You need to declare $HTTP_SESSION_VARS as global in the function (not neccessary with $_SESSION as it is a superglobal):
Code: Select all
function recalculate()
{
global $HTTP_SESSION_VARS;
// rest of function code
}
The same holds true for any $HTTP_xxx_VARS array.
Mac
Posted: Thu Nov 28, 2002 7:07 am
by dmorris
that has solved all the errors I was recieving.
Unfortunately the values only seem to persist over one page, as soon as another page calls the functions the session values have dissapeared.
I have a script which get passed data and the name of the method to call from my shopping cart class. echoing out from the shopping cart script proves that the values are being added. (although obviously breaks the header function)
Once the script has finished it uses
Code: Select all
<?php
header("Location: ../shoppingCart/viewCart.php");
?>
which redirect to viewcart which just calls a function from the shopping cart to echo out all the data. Somewhere between adding the products and viewing the cart the session data is lost.
I am confused - espcially since it works fine with $_SESSION
Any ideas?
Posted: Thu Nov 28, 2002 7:56 am
by twigletmac
You have got session_start() at the top of any page where you try and access session variables?
Mac
Posted: Thu Nov 28, 2002 8:20 am
by dmorris
yeah, got session_start on all pages, - I am really confused, so will post the three relevant pages, sorry for the length, but I haven't found a solution in days
Code: Select all
<?php
<?php
/*
* updateCart.php
*
* Accepts a function name and calls it with the required information - also passed
*
* The user is the redirected to the view cart page
*
*/
if(!session_start()){
echo "Failed when starting session";
exit;
}
include_once("./shoppingCart.php");
$cart = new shoppingCart();
//retrieve variables from the request
if (isset($_REQUESTї'pid'])) { $pid = $_REQUESTї'pid']; }
if (isset($_REQUESTї'method'])) { $method = $_REQUESTї'method']; }
if (isset($_REQUESTї'quant'])) { $quant = $_REQUESTї'quant']; }
switch ($method) {
case "add":
$cart->add_product($pid);
break;
case "update":
$cart->update_product($pid, $quant);
break;
case "delete":
$cart->remove_product($pid);
break;
case "remove_all":
$cart->delete_cart();
break;
}
echo "<a href="../shoppingCart/viewCart.php">view cart</a>";
//header("Location: ../shoppingCart/viewCart.php");
?>
?>
Code: Select all
<?php
<?php
/**************SHOPPINGCART.PHP****************************
*
* This script stores the functions to implement a session based
* shopping cart.
*
* (C) 2002 Duncan Morris (duncan@wandd.net)
*
* Last Modified : 28/11/02
*/
include_once ("../scripts/DB.php");
class shoppingCart {
var $db = "";
var $HTTP_SESSION_VARS;
function shoppingCart(){
global $HTTP_SESSION_VARS;
//called when a new object is created - just open a session
if(!session_start()){
die ("Something has gone wrong with the session in shopping cart");
}
if(!isset($HTTP_SESSION_VARSї'products'])) {
//if cart doesn't exist create it.
echo "products array isn't set";
$HTTP_SESSION_VARSї'products'] = array();
$HTTP_SESSION_VARSї'items'] = 0; //holds the number of items in the cart (stored not calculated cos called on every page).
$HTTP_SESSION_VARSї'total'] = 0.0; //holds the cost of the products (stored not calculated cos I call on every page).
}
//open database connection
$this->db = new DB();
if (!$this->db->open()) {
die($this->db->error());
}
}//shoppingCart
function add_product ($pid){
global $HTTP_SESSION_VARS;
echo "add_product($pid)";
$this->update_product($pid, 1);
}
function update_product ($pid, $quant){
//adds a product to the cart or increments by one if already in cart
global $HTTP_SESSION_VARS;
echo "update_product($pid, $quant)";
$done = false;
$oldQuant = 0;
$len = count($HTTP_SESSION_VARSї'products']);
for ($i=0; $i<$len; $i++){
if ($HTTP_SESSION_VARSї'products']ї$i]ї'pid'] == $pid) {
$oldQuant = $HTTP_SESSION_VARSї'products']ї$i]ї'quant'];
$HTTP_SESSION_VARSї'products']ї$i]ї'quant'] = $quant;
$done = true;
}
}
if(!$done){
$HTTP_SESSION_VARSї'products']ї] = array ("pid" => $pid, "quant" => $quant);
}
$HTTP_SESSION_VARSї'items'] -= $oldQuant;
$HTTP_SESSION_VARSї'items'] += $quant;
$this->recalculate();
}//add_product
function remove_product ($pid){
global $HTTP_SESSION_VARS;
echo "remove_product ($pid)";
$len = count($HTTP_SESSION_VARSї'products']);
for ($i=0; $i<$len; $i++){
if ($HTTP_SESSION_VARSї'products']ї$i]ї'pid'] == $pid) {
//first update the number of items
$HTTP_SESSION_VARSї'items'] -= $HTTP_SESSION_VARSї'products']ї$i]ї'quant'];
//then set the quantity to be 0
unset($HTTP_SESSION_VARSї'products']ї$i]);
}
}
$this->recalculate();
}//remove_product
function get_items (){
global $HTTP_SESSION_VARS;
echo "get_items ()";
return $HTTP_SESSION_VARSї'items'];
}//get_items
function get_total(){
//return the total cost of the cart
global $HTTP_SESSION_VARS;
echo "get_total()";
return $HTTP_SESSION_VARSї'total'];
}//get_total
function get_desc(){
//returns a text summary of the cart.
global $HTTP_SESSION_VARS;
echo "get_desc()";
$desc = "";
foreach ($HTTP_SESSION_VARSї'products'] as $prod){
$query = "Select * from Product where productID = '".$prodї'pid']."'";
if (!$this->db->query($query)){
die($this->db->error());
}
$row = $this->db->fetchAssoc();
$desc .= " ".$prodї'quant']." x ".$rowї'name'].", <br />";
}
return $desc;
}//get_desc
function view_cart(){
global $HTTP_SESSION_VARS;
echo "view_cart()";
//sort out postage out based on cart total.
$postage = 5.00;
if ($this->get_items() == 0){
echo "Your cart is currently empty.";
} else {
//print out the shopping cart
echo "<table border= 0 cellpadding="4" cellspacing="0" width ="100%" >
<tr align="left" bgcolor="#009966"><th align="left" width="100%">Product</th><th align="center">Quantity</th><th align="right">Price</th><th align="right" nowrap>Sub Total</th><th>Delete</th></tr>
";
foreach ($HTTP_SESSION_VARSї'products'] as $prod){
$query = "Select p.*, pc.price as price from Product p, ProductCategory pc where p.pcid = pc.pcid and p.productID = '".$prodї'pid']."'";
if (!$this->db->query($query)){
die($this->db->error());
}
$row = $this->db->fetchAssoc();
echo "
<tr align="center" class="product"><td align="left"><a href="../shoppingCart/product.php?pcid=".$prodї'pid']."">".$rowї'name']."</td>
<td align="center"><form action="../shoppingCart/updateCart.php" method="post"><input type="hidden" name="pid" value="".$prodї'pid'].""><input type="hidden" name="method" value="update"><input type="text" name="quant" size=4 maxlength=4 value=".$prodї'quant']."><input type="image" src="../images/update.gif" value="Update Quantity" height="20" width="35"></form></td>
<td align="right">".$rowї'price']."</td>
<td align="right">". number_format(($prodї'quant'] * $rowї'price']), 2)."</td>
<td><form action="../shoppingCart/updateCart.php" method="post"><input type="hidden" name="pid" value="".$prodї'pid'].""><input type="hidden" name="method" value="delete"><input type="image" src="../images/delete.gif" value="Remove product" height="20" width="35"></form></td></tr>
<tr align="center"><td colspan="5"><img src="../images/spacer.gif" height="2" width="100%"></td></tr>
";
}
$subtotal = $this->get_total();
$total = $subtotal + $postage;
echo "
<tr align="center"><td>&nbsp;</td><td>&nbsp;</td><td align="right" nowrap><B>Sub Total:</B></td><td align="right">".number_format($subtotal, 2)."
<tr align="center"><td>&nbsp;</td><td>&nbsp;</td><td align="right"><B>Postage:</B></td><td align="right">".number_format($postage, 2)."
<tr align="center"><td>&nbsp;</td><td>&nbsp;</td><td align="right"><B>Total:</B></td><td align="right">".number_format($total, 2)."</td><td>&nbsp;</td></tr>
</table>
";
}//else
}//view_cart
//INTERNAL//
function recalculate(){
echo "recalculate() - > get_items() =".$this->get_items();
global $HTTP_SESSION_VARS;
//This is used to update the variables holding the number of items and the price.
//It should decrease the number of database queries since get_total will not need
//to do any connections
$HTTP_SESSION_VARSї'total'] = 0.0;
foreach ($HTTP_SESSION_VARSї'products'] as $prod){
$query = "Select price from ProductCategory pc, Product p where p.pcid = pc.pcid and p.productID = '".$prodї'pid']."'";
if (!$this->db->query($query)){
die($this->db->error());
}
//retrieve the price and add it on to the total
$row = $this->db->fetchAssoc();
$HTTP_SESSION_VARSї'total'] += ($rowї'price'] * $prodї'quant']);
}
}//recalculate
function delete_cart(){
global $HTTP_SESSION_VARS;
echo "delete_cart()";
unset($HTTP_SESSION_VARSї'products']);
unset($HTTP_SESSION_VARSї'items']);
unset($HTTP_SESSION_VARSї'total']);
$HTTP_SESSION_VARSї'products'] = array();
$HTTP_SESSION_VARSї'items'] = 0;
$HTTP_SESSION_VARSї'total'] = 0.0;
unset($HTTP_SESSION_VARSї'cartID']);
}//delete_cart;
}//shoppingCart
?>
?>
Code: Select all
<?php
<?php
include_once ("../scripts/GUI.php");
include_once ("../shoppingCart/shoppingCart.php");
if(!session_start()){
echo "Session failed to open";
exit;
}
$cart = new shoppingCart();
write_header("Shopping Cart", "MustHaveMagnets shopping cart");
write_top();
write_menu();
echo "<h1>Your cart contains :</h1><br />";
$cart->view_cart();
if( $cart->get_items() > 0 ){
echo"
<br /><center>
<A HREF="../shoppingCart/getEmail.php"><img src="../images/checkout.gif" alt="Proceed to Checkout"></a>&nbsp;
<A HREF="../shoppingCart/updateCart.php?method=remove_all"><img src="../images/deletecart.gif" alt="Delete Cart"></a>
</center>
";
}
write_bottom();
?>
?>
Hope someone out there has some ideas,
thank you in advance
Duncan
Posted: Thu Nov 28, 2002 10:37 am
by BigE
One thing I noticed is that in your initalizer function your declaring session_start() when you most likely have already done it in your script that calls the cart code. I suggest that you take the session_start() out of your class and just call it from your scripts that include the class because it could be that the session is getting over wrote due to declaring it twice. Not totally sure if thats the case, but its worth a shot.
Posted: Thu Nov 28, 2002 11:04 am
by dmorris
cheers, but sorry still loses all the session data.
I put that session.start in as something to try, since after pulling my hair out that was all I can think of.
Any other ideas? -- anyone?? --please.....
Duncan
Posted: Fri Nov 29, 2002 1:46 am
by twigletmac
Is the older version 4.0.6? IIRC, a while ago someone had a similar problem and it turned out that they needed to use the
session_register() way of dealing with session variables instead of the immensely better way using $HTTP_SESSION_VARS or $_SESSION.
So you would need to try something like (example code from manual):
Code: Select all
$barney = 'A big purple dinosaur.';
session_register('barney');
instead of
Code: Select all
$HTTP_SESSION_VARSї'barney'] = 'A big purple dinosaur';
and
Code: Select all
if (session_is_registered('barney')) {
instead of
Code: Select all
if (isset($HTTP_SESSION_VARSї'barney'])) {
Mac