Skip to content

'Uninitialized string offset: 0' when using request->referer coming from home page (/) under php7 #8794

@elboletaire

Description

@elboletaire

This is a (multiple allowed):

  • bug
  • enhancement
  • feature-discussion (RFC)
  • CakePHP Version: 3.2.8
  • Platform and Target: PHP 7 on Ubuntu Linux

What you did

Use $this->request->referer(true) when the referer is our home (/) crashes under PHP7 with the error Uninitialized string offset: 0.

The problem is in this line of the Request@referer method:

if ($local && strpos($ref, $base) === 0) {
    $ref = substr($ref, strlen($base));
    if ($ref[0] !== '/') {
        $ref = '/' . $ref;
    }
    return $ref;
}

$ref is an empty string due to the substr($ref, strlen($base)).

Expected Behavior

referer should be returning /

Actual Behavior

Crashes with the error Uninitialized string offset: 0.

I'm just creating a PR with the fix and its test. BTW, I'm not sure about how I've fixed, but let's discuss it on the PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions