php - Add to cart $_SESSION has been destroy after logged in -
why $_session["products"]
has been destroy after logged in, how keep $_session["products"]
after logged in?
add product cart before logged in.
after logged in cart empty.
code
login.php
<?php ob_start(); session_start(); include 'init.php'; require_once 'config.php'; //initalize user class $user_obj = new cl_user(); if(!empty( $_post )){ try { $user_obj = new cl_user(); $data = $user_obj->login( $_post ); if(isset($_session['logged_in']) && $_session['logged_in']){ header('location: home.php'); } } catch (exception $e) { $error = $e->getmessage(); } } if(isset($_session['logged_in']) && $_session['logged_in']){ header('location: home.php'); } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>smart login page</title> <link href='http://fonts.googleapis.com/css?family=pacifico' rel='stylesheet' type='text/css'> <!-- bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/font-awesome.min.css" rel="stylesheet"> <link href="css/login.css" rel="stylesheet"> <script src="js/bootstrap.min.js"></script> </head> <body> <div class="container"> <?php require_once 'templates/ads.php';?> <div class="login-form"> <?php require_once 'templates/message.php';?> <h1 class="text-center">login</h1> <div class="form-header"> <i class="fa fa-user"></i> </div> <form id="login-form" method="post" class="form-signin" role="form" action="<?php echo $_server['php_self']; ?>"> <input name="email" id="email" type="email" class="form-control" placeholder="email" autofocus> <input name="password" id="password" type="password" class="form-control" placeholder="password"> <button class="btn btn-block bt-login" type="submit" id="submit_btn" data-loading-text="loging in....">login</button> <br> </form> <div class="form-footer"> <div class="row"> <div class="col-xs-6 col-sm-6 col-md-6"> <i class="fa fa-lock"></i> <a href="forget_password.php"> forgot password? </a> </div> <div class="col-xs-6 col-sm-6 col-md-6"> <i class="fa fa-check"></i> <a href="register.php"> sign </a> </div> </div> </div> </div> </div> <!-- /container --> <script src="js/jquery.validate.min.js"></script> <script src="js/login.js"></script> </body> </html> <?php ob_end_flush(); ?>
login function in user.php
public function login( array $data ) { $_session['logged_in'] = false; if( !empty( $data ) ){ // trim incoming data: $trimmed_data = array_map('trim', $data); // escape variables security $email = mysqli_real_escape_string( $this->_con, $trimmed_data['email'] ); $password = mysqli_real_escape_string( $this->_con, $trimmed_data['password'] ); if((!$email) || (!$password) ) { throw new exception( login_fields_missing ); } $password = md5( $password ); $query = "select member_id, member_display_name, member_email, member_status, roles_id fm_member member_email = '$email' , member_pwd = '$password' "; //$query = "select user_id, name, email, created, roles_id, id users email = '$email' , password = '$password'" $result = mysqli_query($this->_con, $query); $data = mysqli_fetch_assoc($result); $count = mysqli_num_rows($result); mysqli_close($this->_con); if( $count == 1){ $_session = $data; if($_session['member_status'] == 'activated') { $_session['logged_in'] = true; return true; } else { throw new exception( 'your account deactiavted! <br> please contact adminnistrator more information.' ); $_session['logged_in'] = false; } }else{ throw new exception( login_fail ); } } else{ throw new exception( login_fields_missing ); } }
cart_process.php
session_start(); //start session include_once("config.inc.php"); //include config file setlocale(lc_monetary,"en_us"); // national format (see : http://php.net/money_format) ############# add products session ######################### if(isset($_post["product_code"])) { foreach($_post $key => $value){ $new_product[$key] = filter_var($value, filter_sanitize_string); //create new product array } //we need product name , price database. $statement = $mysqli_conn->prepare("select fm_product.p_name, fm_product.p_price, fm_product.p_member_id, fm_product.p_discount, fm_member.member_display_name, fm_member.member_payment, fm_product_image.img_1, shipping_cost.shipping_register, shipping_cost.shipping_normal, shipping_cost.shipping_ems fm_product left join fm_member on fm_member.member_id = fm_product.p_member_id left join fm_product_image on fm_product_image.p_id_img = fm_product.p_id left join shipping_cost on shipping_cost.shipping_vendor = fm_member.member_id p_id=?"); $statement->bind_param('s', $new_product['product_code']); $statement->execute(); $statement->bind_result($product_name, $product_price, $p_member_id, $p_discount, $member_display_name, $member_payment, $img_1, $shipping_register, $shipping_normal,$shipping_ems); while($statement->fetch()){ $new_product["p_name"] = $product_name; //fetch product name database $new_product["p_price"] = $product_price; $new_product["p_member_id"] = $p_member_id; $new_product["p_discount"] = $p_discount; $new_product["member_display_name"] = $member_display_name; $new_product["member_payment"] = $member_payment; $new_product["img_1"] = $img_1; $new_product["shipping_register"] = $shipping_register; $new_product["shipping_normal"] = $shipping_normal; $new_product["shipping_ems"] = $shipping_ems; //fetch product price database if(isset($_session["products"])){ //if session var exist if(isset($_session["products"][$new_product['product_code']])) //check item exist in products array { unset($_session["products"][$new_product['product_code']]); //unset old item } } $_session["products"][$new_product['product_code']] = $new_product; //update products new item array } $total_items = count($_session["products"]); //count total items die(json_encode(array('items'=>$total_items))); //output json } ################## list products in cart ################### if(isset($_post["load_cart"]) && $_post["load_cart"]==1) { if(isset($_session["products"]) && count($_session["products"])>0){ //if have session variable $cart_box = '<ul class="cart-products-loaded">'; $total = 0; foreach($_session["products"] $product){ //loop though items , prepare html content //set variables use them in html content below $product_name = $product["p_name"]; if(!empty($product["p_discount"])) { $product_price = $product["p_discount"]; } else if(empty($product["p_discount"])) { $product_price = $product["p_price"]; } $product_code = $product["product_code"]; $p_member_id = $product["p_member_id"]; $member_display_name = $product["member_display_name"]; $member_payment = $product["member_payment"]; $product["product_qty"] = 1; $product_qty = $product["product_qty"]; $cart_box .= "<li>$product_name — price ".$product_price." x ".$product_qty." = ".sprintf($product_qty * $product_price)."<a href=\"javascript:void(0);\" class=\"remove-item\" data-code=\"$product_code\">×</a></li>"; $subtotal = ($product_price * $product_qty); $total = ($total + $subtotal); } $cart_box .= "</ul>"; $cart_box .= '<div class="cart-products-total" style="border-top: 1px solid #c0c0c0;">'.$quantity.'total : '.sprintf($total).'<a href="check_out.php" title="review cart , check-out" style="margin-left: 10px;"><u>check out</u></a></div>'; die($cart_box); //exit , output content }else{ die("empty cart!"); //we have empty cart } }
edit
home.php added
<?php session_start(); include('connect.php'); $id = $_session['member_id']; if(!isset($_session['logged_in'])){ header('location: index.php'); } ?> <?php require_once 'templates/header.php';?> <?php if($_session['roles_id']=='1') { ?> <div class="content"> <div class="container"> <div class="col-md-8 col-sm-8 col-xs-12"> <br> <h1 class="text-center"> admin page </h1> <br> </div> <?php require_once 'templates/sidebar.php';?> </div> </div> <!-- /container --> <?php } else if($_session['roles_id']=='2') { ?> <div class="content"> <div class="container"> <div class="col-md-8 col-sm-8 col-xs-12"> <br> <h1 class="text-center"> user page </h1> <br> </div> <?php require_once 'templates/sidebar.php';?> </div> </div> <!-- /container --> <?php } ?>
looks $_session = $data; may culprit, you're resetting entire session variable data.
edit
where $_session = $data change this;
$data["products"] = $_session["products"]; $_session = $data;
Comments
Post a Comment