Answer the question
In order to leave comments, you need to log in
Cookie confusion. Why is the given cookie applied from the second click?
Hello.
--
Prehistory:
I completely lost my patience and strength, tried a lot of options and combinations, included logic. But nothing has changed. I made a self-writing, where a simple task is performed, substitute the name of the desired folder through cookies, depending on which request was made. The request is made by the POST method, it changes the paths to the files. The result of my torment has led to what works on the double click system. The first time you click, the default folder is triggered, because if you remove the default folder, the entire site crumbles. There are three folders, in each file with the same name, but with different variables, the variables change the language of the site. Google translate is not suitable, get requests are not allowed. Plus, it hurts my head so that cookies do not fly after two clicks on links. Keep them for a day. Here is my unfortunate code, I confess, all I could do:
<?php
session_start();
$value = $_POST['languen'];
setcookie("lang", $value);
if($value == '') {$value = 'en';} // если не было post запроса, по умолчанию ставим en
if($_COOKIE["lang"]){$live = $value;} // если куки есть, даём переменной значение $value
elseif(!$_COOKIE["lang"]){$live = 'en';} // если куки нет, возвращаем значение по умолчанию, иначе ошибка
else{$live = $value;} // в противном случае пытаемся отдать переменной то, что отправили через post
?>
<?php require_once ''.$live.'/function.php'; // тут подставляется куки папка ?>
Answer the question
In order to leave comments, you need to log in
More or less like this:
$default = 'en'; // по умолчанию
$allowed = ['en', 'ru', 'fr']; // допустимые языки
$current = empty($_COOKIE["lang"]) ? $default : $_COOKIE["lang"];
$new = empty($_POST["lang"]) ? null : $_POST["lang"];
if ($new && // если был пост запрос
in_array($new, $allowed) && // и если новое значение в списке допустимых
$new !== $current) { // и отличается от текущего
$current = $new;
setcookie("lang", $new, time()+60*60*24); // http://php.net/manual/ru/function.setcookie.php
}
require_once $current . '/function.php';
A month is a very long time...
if ($_COOKIE["lang"]) {
$live = $value; // присваиваете опять пост, а не кукис
}
if ($_COOKIE["lang"]) {
$live = $_COOKIE['lang'];
}
1. if we got lang from post , then we can immediately substitute it into php require_once ''.$live.'/function.php';
and save this value for the future in cookies
2. if we did not receive the value from the post, then by default en
Example:
<?php
session_start();
$data = &$_POST;
# 1
$lang = ($data['languen']) ? $data['languen'] : 'en';
# 2
setcookie('lang', $lang);
# 3
require_once '/' . $lang . '/function.php';
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question