I
I
Igor Vasiliev2017-04-15 01:41:06
PHP
Igor Vasiliev, 2017-04-15 01:41:06

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';  // тут подставляется куки папка ?>

The sequence of variables does not seem to be broken, otherwise, the site would have collapsed, and so the language changes, albeit the second time, and lasts until the second click on the link.
================================================= ================================
Question:
Where is my mistake? What did I do wrong, why is it applied from the second click and disappears too?
How to delay cookies for a day, and how to force them to be applied from the first click. Reading the documentation led to code like the one above. I need to show and explain my mistake so that I understand how it works. I've been fighting for the second month, but the result has not moved. Help me to understand.
--
Thank you in advance for your kindness.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stalker_RED, 2017-04-15
@Isolution666

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';

And it would be better to remove it from the global scope.

O
ozornick, 2017-04-15
@ozornick

A month is a very long time...

if ($_COOKIE["lang"]) {
$live = $value;  // присваиваете опять пост, а не кукис
}
if ($_COOKIE["lang"]) {
$live = $_COOKIE['lang'];
}

Well, of course, you can’t just substitute

D
Denis Artamonov, 2017-04-15
@ArtamonovDenis

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 question

Ask a Question

731 491 924 answers to any question