Deprecated function: explode(): Passing null to parameter #2 ($string) of type string is deprecated

Created on 3 July 2024, 5 months ago
Updated 24 July 2024, 4 months ago

Problem/Motivation

I just noticed a few of these errors in my Watchdog log:

Deprecated function: explode(): Passing null to parameter #2 ($string) of type string is deprecated in Drupal\visitors\Service\CookieService->getId() (line 40 of /var/www/mysite/web/modules/contrib/visitors/src/Service/CookieService.php)

I'm running Drupal 10.3.0 with PHP 8.1.2. I'm not sure what triggered it, but I do see that the IP address associated with the error (66.249.64.164) is for a Google crawler. Here's the call stack:

#0 /var/www/mysite/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real()
#1 [internal function]: _drupal_error_handler()
#2 /var/www/mysite/web/modules/contrib/visitors/src/Service/CookieService.php(40): explode()
#3 /var/www/mysite/web/modules/contrib/visitors/src/Service/TrackerService.php(293): Drupal\visitors\Service\CookieService->getId()
#4 /var/www/mysite/web/modules/contrib/visitors/src/Service/TrackerService.php(157): Drupal\visitors\Service\TrackerService->getVisitorId()
#5 /var/www/mysite/web/modules/contrib/visitors/src/Controller/Visitors.php(60): Drupal\visitors\Service\TrackerService->log()
#6 [internal function]: Drupal\visitors\Controller\Visitors->track()
#7 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#8 /var/www/mysite/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#9 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
#10 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#11 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#12 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#13 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle()
#14 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#15 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#16 /var/www/mysite/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#17 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(191): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#18 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(128): Drupal\page_cache\StackMiddleware\PageCache->fetch()
#19 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\page_cache\StackMiddleware\PageCache->lookup()
#20 /var/www/mysite/web/modules/contrib/cleantalk/src/EventSubscriber/BootSubscriber.php(193): Drupal\page_cache\StackMiddleware\PageCache->handle()
#21 /var/www/mysite/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\cleantalk\EventSubscriber\BootSubscriber->handle()
#22 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#23 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#24 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#25 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#26 /var/www/mysite/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#27 /var/www/mysite/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#28 {main}

Proposed resolution

This is the code:

[$visitor_id] = explode('.', $_pk_id);

The error can be avoided if the call to explode is skipped if the value of $_pk_id is NULL, but I don't know what the value of [$visitor_id] should be set to in that case. Would it make sense to set it to an empty array ([]), like this?

[$visitor_id] = is_null($_pk_id) ? [] : explode('.', $_pk_id);

🐛 Bug report
Status

Fixed

Version

2.19

Component

Code

Created by

🇺🇸United States sah62 US

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @sah62
  • Assigned to abhishek_virasat
  • Merge request !143fix the issue → (Open) created by abhishek_virasat
  • Issue was unassigned.
  • Status changed to Needs review 5 months ago
  • 🇮🇳India abhishek_virasat

    @sah62, I have fixed the issue and created MR. please review it once.

  • Pipeline finished with Failed
    5 months ago
    Total: 183s
    #215347
  • First commit to issue fork.
  • 🇺🇸United States bluegeek9

    Hi @sah62,

    Thank you for reporting the bug. The visitor_id is generated client-side add stored in the cookie. It is used to track unique visitors. I am guessing the Google crawler does not set cookie values.

    `[$visitor_id] = is_string($_pk_id) ? explode('.', $_pk_id) : [];`

  • Pipeline finished with Skipped
    5 months ago
    #216207
    • bluegeek9 committed d3ae1e2f on 8.x-2.x
      Issue #3458872 by bluegeek9, sah62: Deprecated function: explode():...
  • Status changed to Fixed 5 months ago
  • 🇺🇸United States sah62 US

    Thanks for the quick replies. I can confirm that the approach described above eliminates the problem.

  • 🇺🇸United States sah62 US

    Sorry, but I just saw this in my watchdog log:

    Warning: Undefined array key 0 in Drupal\visitors\Service\CookieService->getId() (line 40 of /var/www/mysite/web/modules/contrib/visitors/src/Service/CookieService.php)

    I believe the code should be changed from this:

    [$visitor_id] = is_string($_pk_id) ? explode('.', $_pk_id) : [];
    
    return $visitor_id;

    to this:

    $visitor_id = is_string($_pk_id) ? explode('.', $_pk_id) : [];
    
    return $visitor_id;
  • Status changed to Active 4 months ago
  • 🇺🇸United States bluegeek9

    What about

    [$visitor_id] = is_string($_pk_id) ? explode('.', $_pk_id) : [NULL];
    
    return $visitor_id;
    

    I don't know if more than the visitor_id could be in the _pk_id.

  • 🇺🇸United States sah62 US

    Looking a little deeper: as described in VisitorsCookieInterface.php, the getId() function is supposed to return a string. $visitor_id does not have an initial value. Doing this:

    [$visitor_id] =...

    sets the value of an array element with an undefined index. The function then returns $visitor_id, which has no value. Maybe this is the right thing to do:

    $visitor_id = is_string($_pk_id) ? $_pk_id : "";
    
    return $visitor_id;

    This ensures that the value of $visitor_id is set to a string value.

  • 🇺🇸United States bluegeek9

    Hi @sah62,

    Thank you for reporting the warning. I was able to reproduce the 'Undefined array key 0' message. I added unit tests.

    The javascript tracking code is from the Matomo project. I do not completely understand it.

    This is the value in the cookie. 'fa522cfb5f93fbee' is the unique visitor id. I do not know what '1714423797' is.
    "fa522cfb5f93fbee.1714423797."

  • Pipeline finished with Skipped
    4 months ago
    #221125
  • Status changed to Fixed 4 months ago
    • bluegeek9 committed 5a7332a4 on 8.x-2.x
      Issue #3458872 by bluegeek9, sah62: Deprecated function: explode():...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024