M
M
MrFallen2018-09-27 14:04:56
HTTP Cookies
MrFallen, 2018-09-27 14:04:56

How to store city name in Cookies?

Good afternoon, I just started to master Laravel and faced such a problem.
The user opens the site, selects his city. I write the city name in Cookies via @php in the Blade template. We get a link to a site like this: site.ru/moscow. Everything works great, but... if you change the city to another one (for example, Omsk), then all links on the site lead to the previous selected city (site.ru/moscow), and not site.ru/omsk.
I want to replace that the page immediately after clicking is reloaded and Cookies are updated. There is a suspicion that somehow the Blade template is loading faster than the Cookies are updated.
I suspect that this way of remembering the user's city is bad, maybe there are other not very complicated options?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vyrkmod, 2018-09-27
@vyrkmod

>There is a suspicion that somehow the Blade template is loading faster than the Cookies are updated.
Actually, yes, the cookie is updated when a response is received from the server, and the server draws the page based on the already sent cookie. Option one: redirect to the same page to process the request with a new cookie. Option two: to create links, use the value from the request or from the cookie, if there is no such value in the request.

M
Mykola Ivashchuk, 2018-09-27
@mykolaim

The user opens the site, selects his city.

After that, we send data to the server using ajax, save / update the city for the user in the database, and then we work using the value from the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question