S
S
Sergey Zaigraev2016-08-12 08:34:36
1C-Bitrix
Sergey Zaigraev, 2016-08-12 08:34:36

COOKIES. Why don't the $APPLICATION->set_cookie() and $APPLICATION->get_cookie() methods work?

I'm trying to set an additional cookie for the user, but the cookie is not set, I checked it on another site under the same conditions, then all the rules are set and read. What could be the problem ? Even this simple code doesn't work.

<?require_once($_SERVER['DOCUMENT_ROOT'] . "/bitrix/modules/main/include/prolog_before.php");

   $APPLICATION->set_cookie("USER_CITY","city");

echo "<pre>";
   print_r($_COOKIE);
echo "</pre>";

and in the array with cookies there is no cookie with the key I need, tell me how can I solve this problem?
and the strange thing is that it’s also easy <? setcookie('USER','asdas');?>not to set a cookie, the site has several aliases, while there’s not even a thought what could be the matter ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2016-08-12
@alexey-m-ukolov

Cookies will only become visible after reloading the page for which they should be visible. To check if the cookies have been set correctly, check them on the next page load before they expire. The expiration date of the cookie is set in the expire parameter. It is convenient to check the existence of a cookie by simply calling print_r($_COOKIE);.
php.net/manual/en/function.setcookie.php

A
Ainur Valiev, 2020-03-09
@vaajnur

replacement
require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php');
require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_after.php');
to
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");
helped. By the way, cookies in Bitrix should be written like this
$APPLICATION->set_cookie("favorites",serialize($arElements), time()+60*60*24*30*12*2, "/");
$APPLICATION->get_cookie('favorites')
is still available like this
$_COOKIE["BITRIX_SM_favorites"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question