页面重新加载后会话变量为空 [英] Session variable empty after page reload

查看:63
本文介绍了页面重新加载后会话变量为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的电子商务应用程序购物车页面上的许多会话变量中只有一个以某种方式被破坏.当我将产品与会话 id 一起添加到购物车时,它会创建一个 order_id(使用一些哈希和盐逻辑)并将其放入会话变量中,当我转到购物车页面时,它会根据 order_id 和 session 查询数据库id 来获取购物车中的产品.它第一次获取记录,但在它之后,如果我重新加载页面,它会立即获取一个空的 order_id.关于只有一个会话变量是如何被刷新的,我很困惑.

Only one of my many session variables on the cart page of my e-commerce application is getting destroyed somehow. When i add the product to the cart along with a session id, it creates an order_id(using some hash and salt logic) and puts it in session variable and when i go to the cart page it queries the DB based on both order_id and session id to fetch the products in the cart. For the first time it fetches the records, but immediately after it if i reload the page it gets an empty order_id. I'm pulling my hair on this as to how only one of the session variables is getting flushed.

它只发生在网络服务器上,但在本地主机上完全正常

我的add_to_cart.php(会话逻辑)

if(empty($_SESSION['sessid'])){
    $_SESSION['sessid'] = session_id();
}

    $user_sessid = $_SESSION['sessid'];

  $ip = get_ip_address();



 if(empty($_SESSION['order_id'])){

    $ip = get_ip_address();
    $with_time = strtotime("now");
    $addsalt = substr(md5($ip.uniqid($with_time,true)),0,15);
    $_SESSION['order_id'] = $addsalt;
}

 $order_id = $_SESSION['order_id'];

我的cart.php(仅限会话逻辑)

 $user_sessid = $_SESSION['sessid'];
 $order_id = $_SESSION['order_id'];

 $userid = $_SESSION['user_id'];

 if(empty($userid)){

 $r_type = 1;   

 $q1=<<<SQL
select od.ipaddress, od.modified_ts, od.product_id,od.discount,od.quantity,p.id,p.name,p.price, p.img_dir,p.img_name from orders od, products p where od.order_id='$order_id' and od.user_sessid='$user_sessid' and od.product_id = p.id
 SQL;

} else {
$r_type = 2;

  $q1=<<<SQL
select uc.modified_ts, uc.product_id,uc.discount,uc.quantity,p.id,p.name,p.price, p.img_dir,p.img_name from user_cart uc, products p where uc.userid='$userid' and uc.product_id = p.id
  SQL;


}

 $r1=$db->query($q1);
 $num1 = $r1->num_rows;

当我在立即重新加载购物车页面时回显查询时,它得到一个空的 order_id.

When i'm echoing out the query on immediate reload of the cart page it is getting an empty order_id.

我想知道这种奇怪的事情是如何发生的.请帮忙...

I'm wondering my head off as to how this weird thing is happening. Please help...

更新我在两个页面顶部都包含一个文件,该文件顶部有 session_start,因此它解释了正在创建和使用的 user_sessid.

Update I am including a file on top of both the pages which has session_start on top of it and therefore it explains the user_sessid being created and used.

推荐答案

您可能需要将 session_start() 添加到文件顶部.

You may need to add session_start() to the top of the file.

您说您正在查询会话 ID 和订单 ID,但您也有

You said you were querying on the session id and order id, but you also have

and od.product_id = p.id 

在您的查询结束时.这个逻辑在哪里?我猜您正在使用 $_GET 请求或类似的东西来确定产品 ID.也许这与您的问题有关,可能只是将其从查询中删除?您应该只需要为您的 SQL 查询使用 1 个唯一键,尤其是与您创建的哈希一样唯一.

at the end of your query. Where is this logic? I'm guessing that you are using a $_GET request or something similar to determine the product ID. Maybe this has something to do with your issue, possibly just remove it from the query? You should only need to use 1 unique key for your SQL query, especially as unique as the hash you're creating.

这篇关于页面重新加载后会话变量为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆