如何确定来宾用户的时间长于会话通常存在的时间 [英] How can I identify a guest user for a time longer than session usually exists

查看:102
本文介绍了如何确定来宾用户的时间长于会话通常存在的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用\Session::getId().但是它随时间变化.也许我听不懂这些课程.据我所知,它在php运行时开始,在php代码完成时被删除.另一方面,我读到会话ID存储在cookie中,当用户再次打开您的站点时,会话恢复".那么,为什么我的会话ID这么快过期.如何获得至少一个月没有变化并且被购物车使用的会话ID"?

I know, that I can use \Session::getId(). But it changes form time to time. Maybe I do not understand the sessions. As I know it starts when php runs and it is deleted when php code is finished. On the other hand I read that session id is stored in cookie and when a user open your site again, the session 'restores'. So why in my case the session id expires so quickly. How can I get the 'session id' that does not change at least for a month and that is used by shopping carts?

更新:这个问题变得有些混乱,因为我不想调用某些东西,也不知道这些东西是如何工作的.

Update: The question becomes a little confusing because I do not want how call some things and do not know how these things work.

我想知道如何在一段时间内(通常比会话更长的时间)识别来宾用户并获取其唯一ID.结果,我想要具有功能someFunction,它可以执行以下操作:

I want to know how I can identify a guest user and get its unique id for some period of time (longer than sessions usually exist). In the result I want to have function someFunction which could do the following:

$guestId = someFunction();
$dbRow = Model::findByGuestIdOrNew($guestId);
// now $dbRow contains all previosly save info about a guest user, 
// of it does not exists, create new row by `$guestId`.

在我看来,有一些唯一的会话ID,其存在时间长于会话.当我看到大多数购物车包中的购物车项目都直接保存到会话时,便想到了这种想法.我真的不知道这些包裹存储购物车物品的时间,但是我认为这段时间甚至超过一个星期.这是我在使用此类购物平台时使用的购物车生命周期的这段时间: shopify bigcommerce .我知道他们将购物车中的物品存放超过一周.因此,我想知道他们在创建帐户之前如何识别来宾用户.另外,了解大多数购物车"包装盒中存储购物车物品的时间也是很有用的信息.

It seems to me that there is some unique id of session, which exists for time longer than session. This thought came to me when I've seen that in most cart packages cart items are saved directly to session. I really do not know how long these packages store cart items, but I think this time is even longer than a week. This period of time for lifetime of cart was defined by me while working with such shopping platforms: shopify and bigcommerce. I know they store cart item longer than a week. So I want to know how they identify a guest user before he creates an account. Also It will be a useful information to know how long most 'shopping cart' packages store cart items.

推荐答案

Laravel实现了一种自定义的会话处理方式,但是其逻辑与

Laravel implements a custom way of handling sessions, but the logic is the same as the one described by the PHP Sessions Documentation. Here's an excerpt from that documentation that pretty much explains the logic behind it:

会话是根据唯一的会话ID为单个用户存储数据的简单方法.这可用于在页面请求之间保留状态信息.会话ID通常通过会话Cookie发送到浏览器,并且该ID用于检索现有会话数据.缺少ID或会话Cookie可使PHP知道创建一个新会话,并生成一个新的会话ID.

Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data. The absence of an ID or session cookie lets PHP know to create a new session, and generate a new session ID.

因此,您对会话如何使用ID的理解是正确的,但是会话还有另一个组件可以定义ID的持续时间.会话在预定义的时间段后到期,对于Laravel,可以在config/session.php文件中设置该时间段:

So your understanding of how sessions use IDs is right, but there is a another component to sessions that defines how long they last. Sessions expire after a predefined period of time, which in the case of Laravel, can be set in the config/session.php file:

'lifetime' => 120,

以上默认值表示会话将在用户闲置120分钟后过期.通过设置以下选项,还可以选择在用户关闭浏览器时使会话过期,从而迫使用户在再次打开浏览器时生成新的会话:

The above default value means that the session will expire after 120 minutes of inactivity from the user. There is also the option to expire the session when the user closes the browser, thus forcing a new session to be generated when he opens the browser again, by setting:

'expire_on_close' => true,

您应该阅读 Laravel会话文档,以进一步了解Laravel如何处理会话,以及阅读config/session.php文件中的注释,其中解释了每个配置选项的作用以及可以采用的值.

You should read the Laravel Sessions Documentation for more insight into how Laravel handles sessions, and also read the comments from the config/session.php file which explain what each configuration option does and what values it can take.

上面的内容解释了会话如何在PHP和Laravel中工作,因此,要使会话持续一整个月,您可以将lifetime值设置为:

The above explained how sessions work in PHP and Laravel, so in your case to make the session last for a whole month you could set the lifetime value to:

'lifetime' => 60*24*30 // 60 minutes x 24 hours x 30 days = 43200 minutes

但是,保持会话似乎很长时间,尤其是在会话还包含身份验证信息的情况下.如果要保留购物车中的内容,建议您将其存储在数据库中,当用户下次登录时,只需从数据库中填充会话即可.

But that seems like a really long time to keep a session, especially if the session also contains authentication info. If you want to persist the contents of a shopping cart I suggest you store them in the database and when the user logs in the next time just populate the session from the database.

例如 Cartalyst购物车库(恰好具有非常好的Laravel集成)处理购物车背后的所有逻辑,同时提供一个不错的 sync 方法,该方法使您可以将数据库中存储的购物车项目添加到当前会话中.您可以执行以下操作从数据库中还原购物车:

For example the Cartalyst Cart library (which happens to have really nice Laravel integration) handles all the logic behind a shopping cart, while offering a nice sync method that allows you to add the cart items stored in the database to your current session. You could do the following to restore a cart from the database:

// Get the items from the database
$items = CartItem::where('user_id', Auth::id())->get();

// Sync the items with the session cart
Cart::sync($items);

以上假设您拥有一个已附加CartItem模型的cart_items表,并且您正在使用

The above assumes you have a cart_items table that has a CartItem model attached, and that you're using the Laravel Authentication to handle users sessions, but as you can see it leads to a very easy to implement solution.

这篇关于如何确定来宾用户的时间长于会话通常存在的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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