Search code examples
phpgoogle-chromenginxvalet

PHP commands execute twice when visiting server from Chrome


Update

Since posting the below, I removed redis from the equation by running a similar operation with $_SESSION and incrementing in PHP natively. The same thing happens – but only when the site is viewed in Chrome. In Firefox and Safari, the integer gets incremented correctly, while with Chrome the increment operation happens twice.

Original question:

Trying out Redis using predis with PHP (7.2) locally. Local server is Laravel Valet.

Redis is installed using Homebrew, Predis installed using Composer. Here is my entire index.php file:

<?php
require 'vendor/autoload.php';

$client = new Predis\Client();
// $client->set('value', '10'); // uncomment to reset
$value = $client->incr('value');

print_r($value);

Refreshing the browser on this page to increment the value.

Expected result would be: 11, 12, 13, 14, 15, 16, … etc.

Instead, I’m getting 11, 12, 14, 16, 18, 20, 22, … etc.

The value gets incremented by 2, as though the increment command runs twice; why?

Not sure how to troubleshoot this.


Solution

  • Turns out Chrome sends two requests per page, one of which is to get a favicon. If it doesn’t get a favicon, it’ll keep requesting it.

    I fixed this in this case by adding a favicon to the server, which made everything work as expected.