I want to add a product in the cart for the guest user
I have created on session by inserting the guest user information in database as follows in my header file which is been including on every view as common
<?php $this->load->view('common/header'); ?>
Below it is what I used at the top of my header file
<?php if($this->session->userdata('user_id')=='')
{
$this->home_model->addguestUser();
}
$info = $this->home_model->productinfo($ProductID);
?>
Now this calls the information is used by my home model to create a session according to user_id from database
The first thing I want is to stop to interact with the database because whenever anybody visits my site the data is add as guest in the database which is very dangerous if the number of people visits on my site increased
I want to create session without making any major changes in my current code and I want to create a session with some random variables and then use it to every page
My current flow of data is as follows
Whenever any persons visits my site then the guest user is created automatically.
Then this session is used throughout the site in the static page also
If the user goes and adds product in the user cart it will automatically gets added in the cart as well as in the entry goes in database
If he is the guest user and adds the product in the cart then after he clicks on the checkout page he will first ask to login to his id if he is registered user .
What I want the flow should be
As of now the database interaction for the guest user should not be there i.e if he is guest user then the database use should be not there.
But the session should be created for the guest user also.
For guest user don't add selected product into database but just store in session. Save data in session for guest user. Once they click for checkout ask them for login or register. After login add their saved(saved in session) data in database.
If they don't register or login, session would automatically destroyed when they leave your site.
Hope this is what you want.
Related
I want to allow guest visotors to store items in a shopping cart.
That cart should be taken into the new session, if that guest logs into an existing account.
Currently I save a session ID alongside cart data in my DB.
But as soon as a user logs into their account, the session ID is changed, so I don't have a way of moving the data from one session to the next.
What is the best way to (selectively) move data into a new session?
Try to check the documentation.
There are many type of session usage
https://symfony.com/doc/current/components/http_foundation/session_configuration.html
I want to display different shopping cart from different user session. Here's the details :
The customer can add item(s) to the shopping cart even he/she isn't logged in yet or in other word there's no user session ($_SESSION['User']) is set.
The shipping details form will be displayed if user session
($_SESSION['User']) is set. If the customer want a quick shop and fill the shipping details, the customer can click a button to proceed quick shop without logged in. I can done this by myself using this variable below to create user session after the customer pressed the 'proceed' button :
$_SESSION['User'] = session_id();
I know that the variable above create a random user id to set the user session. After the customer click the 'proceed' button, the system will automatically create user session so the shipping details form will be displayed.
But how if i want to remove Cart's Session if he/she is logged in? Here's some points i know :
If the user is logged in, the $_SESSION['User'] value is the customer's username.
But, if the user isn't logged in but have the user session (after clicking the 'proceed' button), the $_SESSION['User'] value is a random string (because i use
session_id()
function that generate random string to create user session)
The point is, how to remove the cart session from user session (that using random session id) if the user is logged in?
**In other words : **
How to display different shopping cart from different user session?
Thank you.
P.S : Sorry for bad english and my sentences that make you confused
The $_SESSION variable can be used to store the products even if the user is not logged in.
So you can start by creating an array to store the items in the shopping cart:
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = [];
}
An empty array has now been created to store any products you put into it. You store products in the cart using the product identifier from your database:
$_SESSION['cart'][] = 5; // insert the product with id 5
When you want to checkout the cart you can access the customer's cart using $_SESSION['cart'] and retrieve information about the products using the ids located in the cart.
Hope this helps.
We want to achieve a functionality where a user goes to a domain A, puts stuff in their basket. When they go to domain B (also owned by us) the contents of the basket he made in A shows up here too.
How do you achieve this in php? How does site b know the identifier of his session on site a?
We looked at coolblue.be and gsmstore.be for inspiration but are clueless :D
You need to create system which does this:
1.When a user logs in on page A you need to set the session id for him and redirect him to B with this session id.
1.1.Session data needs to be stored in database(sessions can be stored in cookie or database, you will need to set saving in a database).
2.When a user is redirected to B this page gives him another session id.
2.1.you need a table in database for page B with sessions_id from pages A and B
2.2.user is then redirected to page A.
3.The user clicks trough page A and add products to basket
4.After some time user come to page B.
4.1.On first visit app checks if user is checked if he have session id from page a from database table for sessions_id from pages A and B.
4.2.If the user haven't visit page A nothing happend, but if user have session from page A, app gets basked data from page A database for sessions and saves to user session in page B. On page B. In both cases in session is set status that user is checked becase we don't want check on ever pageview
5.DONE!
Addition.
1.Records in database table for sessions_id from pages A and B older than few hours can be deleted.
2.The same thing for page A need to be done for page B in same way.
I have made a PHP application which will be used by many users. When a user logins, i create a session and keep the user id and some other details in the session. There are some ajax requests when the user edits his profile. So in no way i am exposing the user id. But i always refer to the session. Even when the user saves his profile, the controller gets the id from session and then passes it to the model.
Now there is an admin, who should be able to view/edit any profile. This is done. I have a admin page with a user table. When he clicks on any player, it goes to another controller, which creates the session for that user and then admin can edit the profile. Any requests that go from the admin page, the session is first erased keeping the admin variable active and then the user session is appended.
Problem: When the admin tries to view multiple profiles at once, he can do that. But when the admin tries to edit multiple profiles at the same time(open new browser tabs for each user), it fails because the the last tab what he opens sets the new session for that particular user.
How can I get over this scenario ? What options do i have ? Is it possible ?
I don't want to append user'ids on all urls. They are not safe, especially for urls which do update/delete.
The short answer:
if it is under the admin panel you may pass the id of an user, it is safe, you trust your admins right?
Long answer (my own idea):
Store the reference, unique id to the user, and pair it with the session variable.
For instance:
$_SESSION['editing_users'][YOUR_USER_ID] = uniqid();
then print it in the form.
After submitting the form you should get the variable, passed via input with hidden attribute.
Find the unique id in your session array, and get ID of the target user from the key.
I want to create an e-commerce project online. I have this issue :
if the user is not logged in, then the selected products will be automatically added to a default cart under the name of guest001, guest002 etc where guestxxx is a default user.
if the user signs up, then the system will automatically check in his carts data to add it to his new account
when the user then wants to add new products to cart, they will be added directly to his cart.
My questions are :
Should I be using cookies to store the data in the user's machine, so it will be used by the system once the user creates his account? or should I use server side session data?
Should I be creating a default client (guestxxx) in the database once the unknown user adds something to his cart, or just create a cookie to store the data without the need to create a guest user?
I just have little suggestion for you.
If you are using Codeigniter Framework you can use Library Cart Class
or you want make code yourself.
According to my experience. I was used session for store cart while users checkout I add his cart into my database.
Disadvantage of using it while browser is closed your guest cart will start from default (no cart),
If you use cookie for it while browser is closed your guest cart will keep exist. ( store in browser ). But I think you can use both of it. Use cookie for keep cart data and use session for processing cart in your PHP Code. because Cookie is stored in browser, malicious user can do malicious activity of it.
For client i think you can use session or cookie rather than add his data into database
Hope it can help you. I just know little about it.