Notice: Undefined index: Dentifier does not find the error

Asked

Viewed 73 times

0

You’re making this mistake in several parts of my homepage. But I don’t know how to fix it. Follow the error and the corresponding code line.

Notice: Undefined index: identifier in /home3/.../helpers-general.php on line 1112

Part of code containing line 1112:

function foodbakery_set_transient_obj($transient_variable, $data_string, $time = 12) {
    if ( !isset($_COOKIE['identifier']) || $_COOKIE['identifier'] == '' ) {
        setcookie('identifier', uniqid(), time() + (86400 * 30), "/"); // 86400 = 1 day
    }
    $result = '';
    $identifier = '';
    $identifier = $_COOKIE['identifier'];  // <==== linha 1112
    $time_string = $time * HOUR_IN_SECONDS;
    if ( $data_string != '' ) {
        $result = set_transient($identifier . $transient_variable, $data_string, $time_string);
    }
    return $result;
}

2 answers

2


It seems that the error is in the misinterpretation of the HTTP protocol.

What the function setcookie does is set a response header Set-Cookie in the HTTP response that will be sent to the client; while $_COOKIE is a list of cookies existing in current request. In other words, setcookie does not change the value of $_COOKIE of the requisition itself.

To own official documentation says so:

Once cookies have been set, they can be accessed next page load through the array $_COOKIE.

The error then happens when the current request does not have the cookie identifier. You will define its value, but this value will exist only from the next request.

Try to understand the difference between headers Set-Cookie and Cookie.

What you can do is something like:

$identifier = $_COOKIE['identifier'] ?? null;
// Versões anteriores à 7:
// $identifier = isset($_COOKIE['identifier']) ? $_COOKIE['identifier'] : null;

if (is_null($identifier)) {
    $identifier = uniqid();
    setcookie('identifier', $identifier, time() + (86400 * 30), "/");
}

// Use $identifier
  • It already has something like this just below. if ( ! function_exists('foodbakery_get_transient_obj') ) {&#xA;&#xA; function foodbakery_get_transient_obj($transient_variable) {&#xA; //$data_string = get_transient( $transient_variable );&#xA; $identifier = uniqid();&#xA; if ( isset($_COOKIE['identifier']) ) {&#xA; $identifier = $_COOKIE['identifier'];&#xA; }&#xA; if ( false === ( $data_string = get_transient($identifier . $transient_variable) ) ) {&#xA; return false;&#xA; } else {&#xA; return $data_string;&#xA; }&#xA; }&#xA;&#xA;}

  • @So with that, I can assume that you understand what’s wrong and you’ve solved the problem?

  • So, Anderson, that code was already there, it doesn’t include anything. I still don’t understand the reason for the error, because when I enter the Adm user the error does not appear. But on other accounts or computers the error keeps appearing;

  • But it was "there" in a completely different function than the one you put in the question. The error is in the function foodbakery_set_transient_obj and this logic was only used in foodbakery_get_transient_obj. Did you read the full answer? Did you search for the above HTTP headers? What did you not understand?

  • It has nothing to do with one function. I don’t understand exactly why the error is because I don’t know PHP, so I basically don’t know what’s going on there. But for some reason the error stopped showing up. Thanks for the answer.

  • @Mauriciosantos But the error is not PHP, it is HTTP. PHP, in this case, was just the tool to manage HTTP. If you are working with web and have no idea what I said in the reply, I strongly recommend that you stop everything and study the HTTP protocol.

  • Got it, I’ll do it. Thanks for the tip.

Show 2 more comments

-2

Try it like this:

function foodbakery_set_transient_obj($transient_variable, $data_string, $time = 12) {
if ( !isset($_COOKIE['identifier']) || $_COOKIE['identifier'] == '' ) {
    setcookie('identifier', uniqid(), time() + (86400 * 30), "/"); // 86400 = 1 day
}
$result = '';
$identifier = isset($_COOKIE['identifier'])?isset($_COOKIE['identifier']):'';  // <==== linha 1112
$time_string = $time * HOUR_IN_SECONDS;
if ( $data_string != '' ) {
    $result = set_transient($identifier . $transient_variable, $data_string, $time_string);
}
return $result;

}

  • In this case you would omit the error message, but the value of $identifier would be an empty string when the cookie does not exist, which does not seem to make much sense.

  • This is just a way to deviate from Notice, it will have no effect on the code. Obviously it should address the reason why the identifier is empty (for example, disabled cookies)

  • 1

    But the problem is not disabled cookies, but wanting to set and use a cookie in the same request. HTTP does not work like this.

  • The question is about notice. notice warns that it shows a variable that may not have been initialized. It’s not a mistake, just a warning of programming malpractice. In any case, the variable would not be started, since in "if" it sets the cookie using the setcookie function. But php doesn’t know this and marks it as an uninitialized variable. Unless you do a check like the one I mentioned.

  • 1

    There are a number of misunderstandings that may be solved with an issue. What the current answer (if fixed the second isset of line 1112) will do is mask the error, hiding the notice, but still working with wrong values. See the accepted answer for a real problem solution. PS: Note the use of ?? in response, which can be replaced by isset in older versions of PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.