Jump to content

session_save_handler won't work with my mysql queries, & other related questions


etabetapi

Recommended Posts

Hi, I want to do something a little unorthodox (at least, something I've never seen before) with session_save_handler. I've written a shopping cart application from scratch which all works fine so far.

 

The problem I want to use session_save_handler for is this:

 

[*]When a user adds a product to their cart from the catalog, the item quantity is stored in a SESSION cart object.

[*]The item quantity added to the cart is removed from the item quantity field in database.

[*]If the user leaves the site without going through checkout (i.e. their session ends) I want the session_save_handler's destroy method to take the item quantity from the cart and restore it to the item quantity field in database.

 

After reading some tutorials about this function, I did a little test with my custom destroy function, and I was able to get it to echo a session variable I set before destruction, but not put the session variable into my database. There is no error, it simply doesn't go through.

 



<?php
function open(){
return true;
}
function close(){
return true;
}
function read(){
return true;
}
function write(){
    return true;
}
function destroy(){
mysql_connect("localhost","root","") or die ("error connecting to db: ".mysql_error());
mysql_select_db("sweetescapes") or die ("error selecting db: ".mysql_error());
$name = $_SESSION['loginName'];
echo $name;
$query = "INSERT INTO `sweetescapes`.`test` (`id`) VALUES '$name'";
$result = mysql_query($query);
if(!result){
echo "there was an error: ".mysql_error();
return false;
}
return true;
}
function gc(){
    return true;
}
session_set_save_handler
('open','close','read','write','destroy','gc');
session_start();
$_SESSION['loginName'] = "testUser";
session_destroy();
?>

 

So I have two questions 1) What's wrong with my code; and 2) Can I use session_save_handler to do what I want for my shopping cart application or am I doing it all wrong?

 

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.