How to keep session id after login in Laravel 5.1 - laravel

I been using my session id to identify all the items that are added to a shopping cart temporary items table.
What I didn't know is that once the user login into the system the session id changes so I can't query the temporary items table anymore as there is no products containing the new session id value.
I am using file session driver and I was thinking to use db sessions but I think it will be exactly the same.
Any ideas on how to keep the session id after login?

Instead of using user's session_id you can store the cart items in a separate session key.
Like so -
Session::put('cart.items.0', $item_id);
Now you can access it as -
$cart = Session::get('cart');
Not exactly what you asked for, but to me this seems like a better way.

Related

session for multiple devices

I am trying to build an ecommerce site.for the cart functionality I want to alive my cart for multiple devices.For example if I create an account and add some product to my cart using my mobile browser,after that if I use the same account to my pc browser I should see the cart with those product I added using my mobile browser.I donno how to do that or what's the method,can you please help me??
My Preferred Approach
In order to do this, you'll want to create or leverage some sort of profile data source. For example, if you're using a database, you likely have a Users table with a record for each customer. I'd suggest adding a new related table that might look something like this to begin with...
Potential Table Name: UserData
UserID (FK)
DataName (string)
DataValue (string)
So, after logging in, the visitor might add something to their cart. When this happens, you'd have a CartID to work with and then add a record to that table with their UserID and it might look like this in T-SQL.
INSERT INTO [dbo].[UserData] ([UserID],[DataName],[DataValue])
VALUES (12345, 'CartID', 'd501e3de-350c-4c20-92c8-8e71445e1774');
Now, when they log in again anywhere, you'd just query for that UserID to find out if they have a cart pending. If they do, load it up. Just remember to clear that value on checkout.
The beauty of this solution is that you can repurpose it for all kinds of other things.
An Alternative Approach
Just query your Cart table, assuming in your solution you're storing carts before checkout is completed. Find the most recent instance of a cart matching their UserID that hasn't been completed.

How to get number of session in laravel project?

I have a basic laravel project which have login, logout and some others basic public pages.
I would like to count all session for the current time(now time). Any session that should be count login user or visit any public pages.
From this project, I would like to know how many session is running?
What I understand from your question is, you want a feature in your application where you can count number of user that are logged in to the application.
If that's the case then as stated above you can hack a way around by adding a bool column in users table or whatever table you are using to store users, and whenever any user logs in, you change that column value to 1 and when the user logs out you change the column value to 0.
This way you can count the users by using that column where the column value is true or 1.
Hope this solves your problem.

woocommerce cart session duplicating

I have a client who is experiencing issues with their Wordpress/WooCommerce website.
Issue: The website is automatically adding products to the cart (Roughly 40-60). The issue occurs for both logged-in & logged-out users. The issue can occur when trying to login to the My Account section, when adding items to the cart or sometimes after adding items to the cart and then visiting the cart it will override cart items with new random items.
I have noticed that in WC_Session_handler the value for _customer_id is often not unique nor is the other session data.. I have removed all server & front-end caching, searched for any other sessions initialized.
Any help would be appreciated as they are losing business due to customers not being able to remove the items from their cart (As the removed items re-appear quickly)..
Domain Name: thecoffeehopper.com :)
The cart is constructed from data in the wp_woocommerce_sessions and wp_usermeta MySQL tables.
I would run the following sql queries to try and find the source of your problem.
select * from wp_woocommerce_sessions;
select * from wp_usermeta where meta_key like '_woocommerce_persistent_cart_%';
The data from these rows are used to construct the cart. Are the random items found in any of these rows? The rows have a user id or customer id to tell which customer the data belongs to.
The data in these rows are serialized strings and is difficult to read directly so I would use the WordPress CLI tool and apply the function maybe_unserialize() to the SQL results.

Codeigniter remove specific user's session

In Codeigniter, how do I remove specific user's session?
I have set login session like below. And I want to remove specific MemId's LoggedIn(in other words, session for just one user) assuming there are about 1000 users.
$this->session->set_userdata([
'LoggedIn'=>true,'MemId'=>PrimaryKey
]);
When user logged in you are setting two session variables.One is LoggedIn and another 'MemId' which is the primary key of member.So,first retrieve the logged in user member id from database then unset it.Might work properly.
MemId is the member id of user for which you want to remove session.
$this->session->unset_userdata('MemId');

How can I validate current user session id with database stored session ids?

I have found that Code Igniter has already ci_sessions table which stores users session ids in it. I understand the main purpose of that table. Just I want to clarify how I can validate with ci_session table data with my current user login session id?
Basically , I am trying to do User login one place at a time. Multiple login's to be prevented. How can I do this?
You couldn't do that with session ID's since logging in from another location would create a new session ID at that new location.
Store the userID to the session then check it on login.
$this->db->where('userId',$userId);
$result = $this->db->get('ci_session');
if($result->num_rows() > 0)
{
//log out old user, throw error, whatever
} else {
//continue with login
}
By storing the user id in the session data and adding it as a column to the sessions table, you can remove all sessions of that user id on login before the session data is created. This will invalidate any session of the same user. For further explanation on how to do this, see here: https://stackoverflow.com/a/9564433/89211

Resources