0

I would like to implement multiple user login show like gmail login. See below image:

enter image description here

Current I'm using COOKIE to get the USERID. But it only give me the last USERID. not all.

Here is PHP to set COOKIE:

setcookie("cookielogin[userLoginRemembered]", $dataLoginQuery['USERID'] , $time + (60*60*24*7));

What I want is, to show all user ever logged in and display it using COOKIE.

Is it possible?

HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

2 Answers2

3

Because you only store last logged user id into cookie, and it overrides old value

Cookie only stores raw text, so if you want to store a list (array), you have to serialize it (by your own way or using serialize() function). This sample code below uses PHP's serialize():

$lastLoggedUserId = '123';
if (!isset($_COOKIE['cookie_key_for_logged_users'])) {
    $cookieLoggedUserIds = [$lastLoggedUserId];
} else {
// unserialize
    $cookieLoggedUserIds = (array) unserialize($_COOKIE['cookie_key_for_logged_users']);
    $cookieLoggedUserIds[] = $lastLoggedUserId;
}
// just to make sure no duplicated user id to be stored
$cookieLoggedUserIds = array_unique($cookieLoggedUserIds);
setcookie('cookie_key_for_logged_users', serialize($cookieLoggedUserIds));
print_r(unserialize($_COOKIE['cookie_key_for_logged_users']));

I've not tested this code, but it's easy to test and tweak.

Tuan Duong
  • 515
  • 3
  • 7
  • Hi @Tuan How to display the output? – HiDayurie Dave Sep 18 '17 at 03:51
  • @HiDayurieDave: You can get all user IDs from cookie, then query to database to get more information: $cookieLoggedUserIds = (array) unserialize($_COOKIE['cookie_key_for_logged_users']); Or you can tweak the code to store more information to the cookie (like email/image,...) – Tuan Duong Sep 18 '17 at 03:52
  • @HiDayurieDave I've updated the code, you can run and see the output – Tuan Duong Sep 18 '17 at 04:23
  • Question, how to remove specific cookie? – HiDayurie Dave Sep 18 '17 at 05:36
  • @HiDayurieDave: You can simply set the cookie with expiration time in the past: https://stackoverflow.com/questions/686155/remove-a-cookie – Tuan Duong Sep 18 '17 at 05:45
  • I'm using this: setcookie("cookie_key_for_logged_users", "123", time()-3600); but it remove all the cookies. – HiDayurie Dave Sep 18 '17 at 05:48
  • @HiDayurieDave This code works for me: setcookie('cookie_key_for_logged_users', null, -1); – Tuan Duong Sep 18 '17 at 05:51
  • Hi @Tuan, I just want to remove specific cookies not all. Example I only want to remove user: 123. How to do that? – HiDayurie Dave Sep 18 '17 at 05:53
  • Hi @HiDayurieDave. You may look into how to remove one element in array https://stackoverflow.com/questions/369602/delete-an-element-from-an-array. array_splice is your need. Then you remove "123" from the $cookieLoggedUserIds then setcookie again with new $cookieLoggedUserIds. – Tuan Duong Sep 18 '17 at 05:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154641/discussion-between-hidayurie-dave-and-tuan-duong). – HiDayurie Dave Sep 18 '17 at 06:10
1

You need to append the information in the cookie.
There is no append for cookies so what we need to do is read it's current value, add current string and write a new cookie.

$currentvalue = $_COOKIE["cookielogin[userLoginRemembered]"];
If(strpos($currentvalue, $dataLoginQuery['USERID']) !== false){
    Echo "username exist in cookie already";
}else{
    setcookie("cookielogin[userLoginRemembered]", $currentvalue .",". $dataLoginQuery['USERID'] , $time + (60*60*24*7));
//Here I set the value of cookie as current value and dataloginquery.
}

Output:

Var_dump(explode(",", $_COOKIE["cookielogin[userLoginRemembered]"]));
// Dumps the array of usernames that is comma separated.
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Hi @Andreas, how to display the output? – HiDayurie Dave Sep 18 '17 at 03:48
  • @HiDayurie not sure what you mean. You need to echo them. I updated my post with how to access the array of user IDs. – Andreas Sep 18 '17 at 04:02
  • That means you have not set your cookie. You need to set the cookie before trying to output it – Andreas Sep 18 '17 at 04:11
  • I do, I'm using your code: setcookie("cookielogin[userLoginRemembered]", $currentvalue .",". $dataLoginQuery['USERID'] , $time + (60*60*24*7)); – HiDayurie Dave Sep 18 '17 at 04:14
  • I don't know what code you use. Cookies are not just "set" read the manual about them. There is too much missing for me to know what you have done wrong. Have you looked if the browser saved the cookie? – Andreas Sep 18 '17 at 04:30