etabetapi Posted November 26, 2008 Share Posted November 26, 2008 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? Link to comment https://forums.phpfreaks.com/topic/134398-session_save_handler-wont-work-with-my-mysql-queries-other-related-questions/ Share on other sites More sharing options...
etabetapi Posted November 28, 2008 Author Share Posted November 28, 2008 bump Link to comment https://forums.phpfreaks.com/topic/134398-session_save_handler-wont-work-with-my-mysql-queries-other-related-questions/#findComment-701073 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.