Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I identify a guest user for a time longer than session usually exists

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.

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`.

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.

like image 448
FreeLightman Avatar asked Dec 04 '15 23:12

FreeLightman


People also ask

How can the user session be identified?

A separate session identification key, called the user session ID, can be used to maintain session state between the client and a junctioned back-end application server. The user session ID uniquely identifies a specific session for an authenticated user and is stored as part of the user's credential information.

Who is guest user in ServiceNow?

The Guest user is what populates the Caller field of new tickets when a user emails an issue into the system, and that user's email does not currently map to a user record in your ServiceNow instance (either directly in your user table or from your integrated Active Directory).

What is guest user?

The guest account lets other people use your computer without being able to change PC settings, install apps, or access your private files. Do note however that Windows 10 no longer offers a Guest account to share your PC, but you can create a restricted account to emulate that kind of functionality.


1 Answers

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:

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.

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,

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,

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.


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.


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);

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.

like image 81
Bogdan Avatar answered Oct 01 '22 04:10

Bogdan