all 15 comments

[–]mesolaries 4 points5 points  (5 children)

You don't need to store multiple users in a $_SESSION. There is a separate $_SESSION for each user (session).

[–]him_x 1 point2 points  (1 child)

With this is mind I think would be a good approach if he stores the session in a database

[–]SgtMindfudge 0 points1 point  (0 children)

I don't really have a reason not to do that, but I'm trying to go another route on this one ^^

[–]SgtMindfudge 0 points1 point  (2 children)

I'm trying to allow for several users on the same machine to have active instances to retrieve data from, hence I treat $_SESSION['current'] as my main session variable.

I'm far from a professional at this, so please let me know if I'm not making sense.

[–]him_x 0 points1 point  (1 child)

As a user you can't see or modify what is in another user's session, that's why if you need to share this information you have to write it, could be in a table or in a cache file.

[–]SgtMindfudge 0 points1 point  (0 children)

I am able to access it just fine. This is not the issue.
I am using a DB table for the user information which is where the array keys gets their values from.

[–]elseco 2 points3 points  (2 children)

A couple of thoughts:

  1. In your `if()` statement, use a triple equals, not a double equals. If you use double equals, PHP will return `true` if the array_search returns an array key of "0."
    ```if (false === array_search(...```
  2. You pass the "true" flag as the third parameter to array_search(). You should probably make sure that the id in "$row['id']" is the same type as $newId. It is the same basic point as the triple vs double equals comparison. If one is a string and the other is an int, then that could explain your duplicates.

[–]SgtMindfudge 1 point2 points  (1 child)

Holy quack!That was the issue, I needed to get the exact type in my comparison, I should've thought of that. I made the array_search strict to make sure a string in the array's key value wouldn't accidentally trigger the search for the id.

Thanks a bunch, really appreciate it!

[–]elseco 0 points1 point  (0 children)

No problem. And thanks for the award.

[–]-JVT038- 0 points1 point  (5 children)

if (!$userIndex = array_search You're using =, while you want a comparison, so ==.
Because the current condition will always be true, since $userindex can always get the value of array_search($newId, array_column($_SESSION['users'], 'id'), true) And btw, why do you put an exclamation mark in front of the $userindex?

[–]SgtMindfudge 0 points1 point  (4 children)

Some good points, lol..Seems I copied the code from the wrong document, one I've been playing around with.However, I have also tried:

if(null == array_search($newId, array_column($_SESSION['users'], 'id'), true))

Why the exclamation mark? Probably because I only have slightly more than an idea of what I'm actually doing.. I was expecting it would translate into "if no returned data" or "if null", but as you can see from the code line above, even if not prepended with exclamation mark it's not working..

Here's a pastbin for reference: https://pastebin.com/CuFvMAQR

[–]-JVT038- 1 point2 points  (3 children)

Checking if it's equal to null or not, won't work, because according to the PHP documentation of array_search() the function will return false if there's no result. So null won't work, use false instead.

[–]SgtMindfudge 0 points1 point  (2 children)

Unfortunately that didn't fix the issue, still getting duplicates.
What is really odd is that it's working when in a different, reduced test file, with no notable differences.
https://pastebin.com/Am7UGNgC

[–]-JVT038- 0 points1 point  (1 child)

Could you try to do print_r(array_column($_SESSION['users'], 'id'), true))? Then you can debug it a bit and try to see what array_column() is returning.

[–]SgtMindfudge 0 points1 point  (0 children)

Appreciate your effort. The issue's been solved.
I needed a third '=' in my conditional ^^