mshaynerush Posted September 20, 2013 Share Posted September 20, 2013 <code> <?require "./includes/cart_class.php";require_once "./includes/functions.inc.php";session_start();//session_unregister('cart');//exit(); $product_id = $_POST["pid"]; $pcid = $_POST["pcid"]; $product_name = $_POST["product_name"]; $price = $_POST["price"]; $qty = $_POST["qty"]; $att_id = $_POST["att_id"]; $att_value_id = $_POST["att_value_id"]; $pop = $_POST['pop']; /// if value =1 then comes from popup product details page. $page_type = $_POST['page_type']; /// if value =1 then comes from category pages. $stoolH = $_POST["stoolheight"]; $arr_att = array($att_id, $att_value_id); $sql="select product_weight from yp_product where product_id='$product_id'"; $pweight=getSingleResult($sql); if($_SESSION['cart']!=""){ $cart=$_SESSION['cart']; } if($qty==''){ if($min_qty!=""){ $qty=$min_qty; }else{ $qty=1; } } if($qty==0){ $_SESSION['sess_msg']= "QUANTITY CANNOT BE LESS THAN 1"; header("Location: product_detail.php?pid=$pid&pcid=$pcid"); exit(); } if(!session_is_registered('cart')){ $cart = new cart(); session_register('cart'); } $item_object = new product($product_id , $product_name , $price, $arr_att, $qty , $pweight, $stoolH); $cart->addToCart($item_object);$back="modify_cart_frm.php";header("Location: ".$back);exit;?></code> it gives a fatal error. Problem started a week or two ago, but the code hasn't changed to my knowledge. I did not write this. Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/ Share on other sites More sharing options...
mshaynerush Posted September 20, 2013 Author Share Posted September 20, 2013 This is the error: Fatal error: Call to a member function addToCart() on a non-object in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 50 Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450341 Share on other sites More sharing options...
mac_gyver Posted September 20, 2013 Share Posted September 20, 2013 the error means exactly what it says, $cart is a non-object, but the code expects it to be one, an instance of your cart class. the code is using two depreciated/removed functions - session_register() and session_is_registered(). however, if your php version was updated to the point where they don't exist at all, you would be getting a fatal runtime error at the first one of them and you would never get to line 50 in the file. for php versions (5.4 and higher) where these functions have been removed, the code will need to be rewritten to either just use the session variable directly to hold an instance of your cart class or you will need to copy the $cart variable out-of/in-to the session variable. afaik, for previous php versions, session_register() 'worked', even with register_globals turned off, despite what the php documentation states (i tested this myself at one point in time), and the code should be registering the $cart variable as a session variable. so it would appear that the session variable exists, but has been overwritten with something other than an instance of your cart class. for some first steps at debugging, do the following - 1) set php's error_reporting to E_ALL and display_errors to ON, immediately after the first opening <?php tag on that page. 2) on line 49 in that file, use var_dump($cart); to see exactly what is in the $cart variable at that point. Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450400 Share on other sites More sharing options...
mshaynerush Posted September 21, 2013 Author Share Posted September 21, 2013 Thank you for that. this is the error i get after all that: Notice: Undefined index: att_id in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 18Notice: Undefined index: pop in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 20Notice: Undefined index: page_type in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 21Notice: Undefined index: stoolheight in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 22Notice: Undefined index: cart in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 30Deprecated: Function session_is_registered() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 49Deprecated: Function session_register() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 51object(cart)#1 (1) { ["cart"]=> array(0) { } }Deprecated: Function session_unregister() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/includes/cart_class.php on line 211Deprecated: Function session_register() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/includes/cart_class.php on line 213Warning: Cannot modify header information - headers already sent by (output started at /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php:51) in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 60 Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450580 Share on other sites More sharing options...
mac_gyver Posted September 22, 2013 Share Posted September 22, 2013 the only thing i can tell you from that (besides that the code needs to be completely rewritten in the near future) is - something about the problem changed as $cart is now an object and the original error isn't being produced. can you reproduce the conditions that led up to the error in the original post and show what the var_dump($cart); statement displays for that case? Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450668 Share on other sites More sharing options...
mshaynerush Posted September 22, 2013 Author Share Posted September 22, 2013 I have no way of doing that because I really don't know. Weeks ago, he was receiving orders and now he can't suddenly. I found some stuff about deprecated tags, changed out session_register for $_SESSION['cart]' and session_unregister with unset($_SESSION['cart']) and that removed the fatal errors. NOW all that happens is that the cart is empty. NO errors, but nothing is being added to the cart. this is my most recent, fatal error free code: <? ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); require "./includes/cart_class.php"; require_once "./includes/functions.inc.php"; session_start(); //unset($_SESSION['cart']; //exit(); $product_id = $_REQUEST["pid"]; $pcid = $_REQUEST['pcid']; $product_name = $_REQUEST['product_name']; $price = $_REQUEST['price']; $qty = $_REQUEST['qty']; $att_id = $_REQUEST['att_id']; $att_value_id = $_REQUEST['att_value_id']; $pop = $_REQUEST["pop"]; /// if value =1 then comes from popup product details page. $page_type = $_REQUEST['page_type']; /// if value =1 then comes from category pages. $stoolH = $_REQUEST['stoolheight']; $arr_att = array($att_id, $att_value_id); $sql="select product_weight from yp_product where product_id='$product_id'"; $pweight=getSingleResult($sql); if($_SESSION['cart']!=""){ $cart=$_SESSION['cart']; } if($qty==''){ if($min_qty!=""){ $qty=$min_qty; }else{ $qty=1; } } if($qty==0){ $_SESSION['sess_msg']= "QUANTITY CANNOT BE LESS THAN 1"; header("Location: product_detail.php?pid=$pid&pcid=$pcid"); exit(); } if(!$_SESSION['cart']){ $cart = new cart(); $_SESSION['cart']; } $item_object = new product($product_id , $product_name , $price, $arr_att, $qty , $pweight, $stoolH); $cart->addToCart($item_object , $product_id , $product_name , $price , $arr_att , $qty , $stoolH , $pweight); $back="modify_cart_frm.php"; header("Location: ".$back); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450734 Share on other sites More sharing options...
mac_gyver Posted September 22, 2013 Share Posted September 22, 2013 (edited) because $cart is no longer 'registered' as a being associated with a session variable, changes to it don't get saved back to $_SESSION['cart']. the most straight-froward fix would be to use $_SESSION['cart'] = $cart; after any statements that modify $cart. edit: btw - you can replace those two bits of $_SESSION['cart'] related logic with just the following - if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = new cart(); } $cart=$_SESSION['cart']; Edited September 22, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450738 Share on other sites More sharing options...
mshaynerush Posted September 23, 2013 Author Share Posted September 23, 2013 Yep. That did the trick. Thank you guys very much for you help. Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450744 Share on other sites More sharing options...
mshaynerush Posted September 23, 2013 Author Share Posted September 23, 2013 Quick question. I have a cannot redeclare error now. It's like the site started falling apart. Anyway, is it necessary to unset a session to avoid this error? Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450745 Share on other sites More sharing options...
mac_gyver Posted September 23, 2013 Share Posted September 23, 2013 cannot help you with your error unless you post it, along with the line of code it refers to. the only redeclare errors would be for function and class names, either being same as a built in function or class or code defining a function or class more than once. Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450746 Share on other sites More sharing options...
mshaynerush Posted September 23, 2013 Author Share Posted September 23, 2013 In the page, there was a drop down menu populated by state names that were then converted from actual state names to abbreviations. I moved that function from the actual page to the functions.inc.php file and then made a function call in place of where convert_state() function was. That fixed the problem. Boy what a pain this was. PHP was fine. I had been working on this site, mostly front end and some small PHP code here and there for years. It just now started doing all of this, but with your help here, I am proud and elated to announce that your help and Google, all errors are gone. Awesome! Thank you for your time and expertise in helping me out with this issue! Quote Link to comment https://forums.phpfreaks.com/topic/282301-need-help-with-this-script/#findComment-1450747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.