When visiting a site, information about this visit is recorded in the database, including the unique user id , which is stored in the cookie. If the user has previously visited this site and his id is already stored in cookies, then everything is normally recorded in the database. If the user is the first time on the site and the cookie is only created at the time of this visit, then this id will not be recorded in the database. Tell me, please, the reasons why this may occur.

  • and what prevents to write data to the database before creating cookies? - mazanax
  • One of the possible reasons - I want to save in the database only users who have visited the site more than once. - xEdelweiss
  • Well, at least the code in the studio - Ale_x

1 answer 1

I understand that you have something that takes an id from a cookie and writes it into a database. If the id is not in the cookie, you do this (in one action): create id -> write it in cookie -> take it from the cookie and write it to the database. The solution is obvious: in case the cookie has not been created yet, do not try to take data for the database from the cookie, but take it from where the user id is created.

  • one
    In general, everything is simple, the reasons can be found only by reading the description of the setcookie function: ".. the documents will be invisible until the next page is loaded." In the decision, yes, as suggested here, to operate further on the variable in which the new id is created. Thank you all :) if (! Isset ($ _ COOKIE ['visitorid'])) {$ visitorid = md5 (uniqid (rand (), 25)); setcookie ("visitorid", $ visitorid, 0x6FFFFFFF); } else $ visitorid = $ _COOKIE ['visitorid']; - devmonkey