WebCitz Blog


Fixing a Read Session Error When Upgrading from PHP 7.0

Fixing a Read Session Error When Upgrading from PHP 7.0
Jake Friday
Jake Friday
May 8, 2019
Posted in  Hosting

If you are having session read errors when upgrading from PHP 7.0 to PHP 7.1/7.2, you may be experiencing this problem when overriding the session _read function. These types of errors can be grueling to track down and fix within your web hosting service. After hours of troubleshooting, we were able to find a solution to this problem. We hope this quick and easy guide helps you fix the “Failed to read session data:” error that you have been getting, and allows you to finally make that PHP upgrade you have been waiting to do.

Your session’s _read function should look similar to this:

public function _read( $id ) {

    //Set the database query
    $this->db->query( 'SELECT data FROM sessions WHERE id = :id' );
    
    //Bind the id parameter
    $this->db->bind( ':id', $id );

    //Attempt execution of query
    //If execution is successful
    if( $this->db->execute() ){

        // Save returned row
        $row = $this->db->single();

        // Return the data
        return $row[ 'data' ];

    }

    //If execution is not successful
    else {

        // Return an empty string
        return '';

    }

}

The reason this error occurs is that PHP versions 7.1 and above now strictly require the session _read to return a string value, which excludes even returning a null value. The code below is where a null value has the possibility of being returned (when $row[ ‘data’ ] is actually null):

//Attempt execution of query
//If execution is successful
if( $this->db->execute() ){

    // Save returned row
    $row = $this->db->single();

    // Return the data
    return $row[ 'data' ];

}

You’ll see the else statement right below is returning ” upon failure of execution. However, there is no check as to whether or not $row[ ‘data’ ] is returning now. All you have to simply do is return ” when $row[ ‘data’ ] is null, resulting in the following edit to the code above:

//Attempt execution of query
//If execution is successful
if( $this->db->execute() ){

    // Save returned row
    $row = $this->db->single();

    //Return a blank string if $row[ 'data' ] is null
    if ( is_null( $row[ 'data' ] ) ) {

        return '';

    }

    //Else return $row[ 'data' ]
    else {

        // Return the data
        return $row[ 'data' ];

    }
}

Your final snippet of code should look like this:

public function _read( $id ) {

    //Set the database query
    $this->db->query( 'SELECT data FROM sessions WHERE id = :id' );

    //Bind the id parameter
    $this->db->bind( ':id', $id );

    //Attempt execution of query
    //If execution is successful
    if( $this->db->execute() ){

        // Save returned row
        $row = $this->db->single();

        //Return a blank string if $row[ 'data' ] is null
        if ( is_null( $row[ 'data' ] ) ) {

            return '';

        }

        //Else return $row[ 'data' ]
        else {

            // Return the data
            return $row[ 'data' ];

        }

    }
    //If execution is not successful
    else {

        // Return an empty string
        return '';

    }

}


Related Topics: