Why is the first id recorded in cookie many times?

The record in cook viewed the lots on the website, but only if the cook still does not contain the id of the lot. That is all the id in the cookie should be unique.
I have, for some reason, the uniqueness is observed only for the second and subsequent entries in the cookie. That is, if I clean cookies I go to the page with id=1 and click f5, then all the updates are written to the cookie. E. T. cook is to have 1,1,1,1,1, etc. But starting with the second element of a uniqueness constraint is already being enforced. Ie if after cook already contains 1,1,1,1,1 to go to the page c other id , it is already written in the cookie only once.
if(isset($_COOKIE['lot_history'])) {
 $lot_history = json_decode($_COOKIE['lot_history']);
 if (!(array_search($_GET['id']-1, $lot_history))){
 $lot_history[] = $_GET['id']-1;
 $lot_history = json_encode($lot_history);
 setcookie("lot_history", $lot_history, strtotime("+1 day"), "/");
 else {
 $lot_history = json_encode(array($_GET['id']-1));
 setcookie("lot_history", $lot_history, strtotime("+1 day"), "/");
June 5th 19 at 21:40
3 answers
June 5th 19 at 21:42
array_search can return both false and 0 (the index of the element). In Your situation, just returns 0 and that in non-strict checking is interpreted as false.
Just do strict checking ...
if (array_search($_GET['id']-1, $lot_history) === false){

P. S. and from such horror as $_GET['id']-1 throughout the code need to get rid of..
Thanks, really helped.
And on account of the $_GET['id']-1 not entirely clear. First - why is it terrible? And how better to get rid of it?
Let me explain: I assign id to elements starting with 1 and array indexing starts from 0. - Mikel64 commented on June 5th 19 at 21:45
horror because:
1. Why are You in 3 places write the same thing $_GET['id']-1 ? If you want to count something - count once and use it in your response just showed it
2. What is the meaning of ID to take 1? why? - bryana.Renn commented on June 5th 19 at 21:48
the first point is clear, thank you, I agree.
As for the second: so id I have are numbered starting with 1, and are stored starting from 0. That is, the first id corresponds to the zero element of the array. By the way a sane answer as to why I numbered id from scratch - I can't give. Just thought that ?id=0 in the link will look ugly. I'm just learning and doing now an academic project, so I will be glad if you tell me, is it permissible to use the reference numbering from zero ) - Mikel64 commented on June 5th 19 at 21:51
in identifiers usually start at 1 not zero.... - bryana.Renn commented on June 5th 19 at 21:54
June 5th 19 at 21:44
I agree with Maxim, but I would also add that cookies have limitations and after is likely that nginx will throw a 500 error because it can not read the headers.

Easier need
$maxHistory = 20;
$addId = $_GET['id'] - 1;

$lot_history = isset($_COOKIE['lot_history']) ? (array)json_decode($_COOKIE['lot_history']) : [];
$lot_history[] = $addId;
$lot_history = array_slice(array_unique($lot_history), -$maxHistory);

setcookie("lot_history", $lot_history, strtotime("+1 day"), "/");</code-->
June 5th 19 at 21:46
Thank you for the answers. That would not create a new question, I will ask one more here, directly related to the topic:
If the user types in a get request id of a non-existent lot, how best to proceed : initially writing it in a cookie, and then on the page display the items just do not show it does not exist?
404 page? - Mikel64 commented on June 5th 19 at 21:49
not quite understand what you mean. This suggestion? Or question me if I know what it is? ))
I load a page with a list of viewed lots, so 404 there is likely not suitable. To weed out lots are not in the database. I see two options - either initially to check for the presence id in the database, and if not, then do not write them to cook. Or burn it all to cook, but before output to check that I bring.

404 can be used when viewing the lots , and if the request is invalid lot to load the 404 page(although, I like the option to load just a basic layout of a page without any output, or error output). In principle, this would mean - do not write originally in cook.

Actually this was the question the question - which way to go better. - bryana.Renn commented on June 5th 19 at 21:52
proposal. 1. You write in the cookie where was the man, so he needs to go to a page, on this page, and should like to record, so if people did not get to the page and write nothing. 2. People can stop by tomorrow and a lot at that time concealed/removed, and then it turns out you need to check there is a lot at the moment or not. - Mikel64 commented on June 5th 19 at 21:55

Find more questions by tags PHPHTTP Cookies