简体   繁体   English

PHP:丢失会话数据(有时)

[英]PHP : Lost Session data (in sometimes)

I'm working on a project with CodeIgniter. 我正在使用CodeIgniter进行项目。 Because CI Session class is using Cookie to store data, and Cookie can hold only 4KB of data, so I decided to use Native PHP Session for storing data instead. 由于CI Session类使用Cookie来存储数据,而Cookie只能容纳4KB数据,因此我决定改用Native PHP Session来存储数据。

My problem is: sometimes the data stored in Session lost but I dont have any ideas about what is causing it. 我的问题是:有时Session中存储的数据丢失了,但是我对导致它的原因一无所知。

Let me explain what i've done in more detail: 让我更详细地说明我所做的事情:

  1. In product detail page : Customer decides to buy this product -> He/she clicks on "Add to cart" button -> Product's information will be stored in Session. 在产品详细信息页面中:客户决定购买此产品->他/她单击“添加到购物车”按钮->产品的信息将存储在Session中。 Now let's click "Next step" button 现在,我们单击“下一步”按钮

  2. Go to "View cart" page: I use data stored in Session (product_id, for example) and get some extra data from database to display for customer. 转到“查看购物车”页面:我使用存储在Session中的数据(例如product_id),并从数据库中获取一些额外的数据以显示给客户。 Now let's click "Next step" button 现在,我们单击“下一步”按钮

  3. In this step, customer has to leave his/her informations (name, phone, email, address) for delivery and I save them in a Session variable names $_SESSION['customer_info']. 在此步骤中,客户必须保留其信息(姓名,电话,电子邮件,地址)以进行传递,我将其保存在名为$ _SESSION ['customer_info']的Session变量中。 Now let's click "Next step" button 现在,我们单击“下一步”按钮

  4. In this step, customer will select a payment method, review his/her order detail and delivery information (it means SESSION is working). 在此步骤中,客户将选择一种付款方式,查看其订单详细信息和交货信息(这意味着SESSION正在运行)。 Let's click on "Checkout" button and move to final step 让我们点击“结帐”按钮,然后转到最后一步

  5. When customer hits "Checkout", I call an AJAX to POST the payment_method to, for example, "frontend/cart/checkout" function. 当客户点击“结帐”时,我会调用一个AJAX来将payment_method发布到“前端/购物车/结帐”功能中。

In this function, I pull out the informations stored in SESSION (cart items, customer information) and save them to the database and then send an email about the order detail to customer. 在此功能中,我提取出存储在SESSION中的信息(购物车项目,客户信息)并将其保存到数据库中,然后向客户发送有关订单详细信息的电子邮件。 The email content contains these informations : products (quantity, amount, name,...) and customer's delivery information (name, address, email, phone). 电子邮件内容包含以下信息:产品(数量,数量,名称等)和客户的送货信息(名称,地址,电子邮件,电话)。 I also save the email content to the database (just for make sure that I wont lose any information). 我还将电子邮件内容保存到数据库中(以确保不会丢失任何信息)。

The problem is : Sometimes (just in sometimes), the delivery informations is empty (I mean there's nothing about the delivery information in the email, or even in the database, it's totally empty) but the cart items are still there. 问题是:有时(只是有时),交货信息为空(我的意思是电子邮件中甚至数据库中都没有关于交货信息的信息,这完全是空的),但购物车物品仍然存在。 :( It's really really weird to me. :(对我来说真的很奇怪。

Any ideas or suggestions? 有什么想法或建议吗?

Thanks in advance! 提前致谢!

The solution in your case is to store session in database also making some config settings in config.php . 您的解决方案是将会话store sessiondatabase并在config.php进行一些config settings


Steps: 脚步:

1) For saving session data in database you have to create a table in your database ci_sessions . 1)为了将会话session data保存在database您必须在数据库ci_sessions创建一个表。 Following is the query (MySQL) for creating it, 以下是用于创建查询的查询(MySQL),

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
   session_id varchar(40) DEFAULT '0' NOT NULL,
   ip_address varchar(45) DEFAULT '0' NOT NULL,
   user_agent varchar(120) NOT NULL,
   last_activity int(10) unsigned DEFAULT 0 NOT NULL,
   user_data text NOT NULL,
   PRIMARY KEY (session_id),
   KEY `last_activity_idx` (`last_activity`)
);


2) Once you have created your database table you can enable the database option in your config.php . 2)创建数据库表后,可以在config.php启用database选项。

$config['sess_use_database'] = TRUE;

Now the session data will get stored into the database. 现在,会话数据将被存储到数据库中。


3) Also make sure you've following config settings: 3)还要确保您遵循以下配置设置:

$config['sess_cookie_name']     = 'ci_session';   // cookie name
$config['sess_expiration']      = 0;              // 0 means non-expiring session
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;           // set TRUE to use database
$config['sess_table_name']      = 'ci_sessions';  // DB table name
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  =  86400 * 30;    // 30 days


Usage of Session class: 会话类的用法:

Also I saw you are still using $_SESSION[''] to store or retrieve session data. 我还看到您仍在使用$_SESSION['']来存储或检索会话数据。 Kindly follow the session class of Codeigniter. 请遵循Codeigniter的session class

1) Add session data. 1)添加会话数据。

$this->session->set_userdata('some_name', 'some_value');


2) Retrieve session data. 2)检索会话数据。

$this->session->userdata('item');


3) Retrieve all session data (returns an array of session data). 3)检索所有会话数据(返回会话数据的数组)。

$this->session->all_userdata();


4) Removing session data. 4)删除会话数据。

$this->session->unset_userdata('some_name');


5) Destroying a session. 5)销毁会话。

$this->session->sess_destroy();


Follow documentation: 请遵循文档:

http://ellislab.com/codeigniter/user-guide/libraries/sessions.html http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

There is a probability that PHP will clean your session data by the garbage collection of the built-in session function. PHP可能会通过内置会话功能的垃圾回收来清理您的会话数据。 If you have thousands or even more visitors everyday, you will observe your session data lost easier. 如果您每天有成千上万甚至更多的访问者,您会发现会话数据丢失更容易。

Just a hint, might http://php.net/manual/en/session.configuration.php#ini.session.gc-divisor would help. 只是一个提示, http ://php.net/manual/en/session.configuration.php#ini.session.gc-divisor可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM