Cross domain shopping basket - php

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.

Related

how to redirect from one website to another website user home page after logging in?

I have 2 websites A & B. Every user has same login password for both websites A & B.
Now I want some specific users to land into(redirect to) B website's user home page after they login into the A website.
I dont want them to open the B website's login page separately for logging in.
If that specific user has logged in A website then he will be redirected to B website's user home page without doin login on B website. Can anybody help me out how to achieve this ?
EDIT - is there any way where i can post the login id password of user from A website's login page to B website's login page and then it fires the login button to redirect the user to B website's user home page in one go ?
Also, if i want to login into some external website for eg. like Gmail through my portal. Is there any way that the user logs in to my website and gets redirected to his Gmail home page ?
You can do this with using few different methods.
Method 1: Using Cookies
Once the user is logged in to Site A you can write a cookie:
setcookie("SiteALoginTrue","True",time()+3600,"/",".Site-B-URL.com");
header("Location : Site-B-URL.com); // Redirect the website to siteB
and in your Site B code you will need to read the cookie while website loads.
Method 2: Using PHP
in your SiteA login script you can save logged user IP with timestamp and after redirected to Site B you will need to another script that checks Site A database if the visted IP is logged in = true. Once site B finds that IP is marked Logged in database then you can create a logged in Session to avoid checking database every page refresh.
If you can connect Site A's DB via Site B
Then this suggestion will help you.
1) On site A
Redirection when user with email user#gmail.com is successfully logged in
$userEmail = md5(user#gmail.com);
// Store below key to DB table of users whose email is user#gmail.com
$key = md5(microtime().rand());
header('Location: http://www.site-B.com/'."$key/$userEmail");
exit;
2) On site B,
Get $key and $userEmail
Connect with site A's DB. and Run query like,
SELECT * FROM users WHERE MD5(email) = '$userEmail' AND key = '$key'
Get user and set session on site B. Then remove key from DB.

PHP Overwrite Cookie Issue

Here's my issue.
I have a site where my users have a page setup for them that displays their information (basically a public profile page) Ex. example.com/johnsmith, example.com/janesmith
When you visit example.com/johnsmith, I used PHP to create a cookie that stores some of John Smiths information, so that when pages outside of John Smith's are being viewed his information will persist across the entire website (example in a banner at the top of the website), so that users can quickly navigate back to John's page.
However, if someone visits example.com/johnsmith and then goes to example.com/janesmith and then clicks back to say the home page, John's information will still display despite the cookie being overwritten with Jane's information until you manually refresh the home page, at which point Jane's information displays correctly.
Any ideas as to what the issue could be causing the overwritten cookie to not display information on other pages until a manual refresh is performed

Guest user cart in codigniter

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.

Admin access to edit profile of any user - Issue with session

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.

set cookie values dynamically for incoming user in ecommerce site

I am trying to show recently viewed items in my site in php.For which i was thinking to make use of cookies,according to which i want to store what user is clicking in a db with cookie values the users have.Now I dont know how to generate cookies dynamically for one pc or device,when user clicks items on my site while navigating through....
Please guide me on how to put cookie values in a db along with product details in database,so that later when they come to my site they should get recently visited products on basis of the cookie values in my db..

Categories