M
M
Max Brilliant2020-07-28 04:09:55
HTTP Cookies
Max Brilliant, 2020-07-28 04:09:55

The simplest work with cookies?

Hello. Tell me please.
I've looked all over the internet and it's full of rubbish everywhere. I need the simplest work with cookies. What do I want to do.
I have a simple site and on it the choice of the city. I want to change phone numbers and addresses when choosing a city.

I would have the most elementary way to record a person by clicking on a city, record in cookies, and due to these cookies, some kind of condition would be used ala if, else.

You need to write cookies and, for example, insert them into input by name. Yab figured it out. I start reading on the Internet, there are articles with thousands of lines, a bunch of filters and algorithms, etc. unnecessary. I would even like

document.cookie = 'name=msk';
document.cookie = 'name=spb';

I know that it overwrites each other. For example, a person clicked on Moscow and "msk" was written in the cookies,
respectively, he goes to the contacts page and there, in the if conditions, the contacts he needs would be given out. What is the easiest way to implement this without writing 50 lines of code?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stas Khitrich, 2020-07-28
@Snova_s_vami

Oops, accidentally sent before writing the full answer. In general, I recently did it on WordPress, the essence is simple, we check the cookie value in PHP like this and write a suitable value to some variable based on the value of the cookie. Nothing complicated.

$default_phone = get_theme_mod('phone_default'); 
$moscow_phone = get_theme_mod('phone_moscow');
$spb_phone = get_theme_mod('phone_spb');
$voronezh_phone = get_theme_mod('phone_voronezh');

$user_region = in_array('uLocation', $_COOKIE) ? $_COOKIE['uLocation'] : null;
$active_phone = null;

if ($user_region) {
    if ($user_region === 'Москва' && $moscow_phone) $active_phone = $moscow_phone;
    else if ($user_region === 'Санкт-Петербург' && $spb_phone) $active_phone = $spb_phone;
    else if ($user_region === 'Воронежская' && $voronezh_phone) $active_phone = $voronezh_phone;
    else $active_phone = $default_phone;
} else {
    $active_phone = $default_phone;
}

In terms of the script that creates the cookie, I have a couple of checks, I checked the city by IP and set the region using DDATA, in your case, as I understand it, everything is simpler, but the essence is this, write down the cookie, but do not forget about the checks that it already installed so as not to load once again. The example below simply sets the cookie by name/value + expires for the date to reset once a week. You can do without it. Here:
const cookieName = 'uLocation';
const addCookie = (cookieVal) => 
   document.cookie = `${cookieName}=${cookieVal.trim()}; expires=${new Date(new Date().getTime() + 3600 * 1000 * 24 * 7).toUTCString()}`;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question