Home Web Hosting Fixing a Learn Session Error When Upgrading from PHP 7.0

Fixing a Learn Session Error When Upgrading from PHP 7.0

by rigotechnology

If you’re having session learn errors when upgrading from PHP 7.0 to PHP 7.1/7.2, chances are you’ll be experiencing this downside when overriding the session _read perform. All these errors might be grueling to trace down and repair inside your website hosting service and may result in safety points for those who don’t treatment them, see our 25 suggestions for internet host administration safety to study extra. After hours of troubleshooting, we had been capable of finding an answer to this downside. We hope this fast and straightforward information helps you repair the “Didn’t learn session information:” error that you’ve got been getting, and means that you can lastly make that PHP improve you have got been ready to do.

Your session’s _read perform ought to look much like this:

public perform _read( $id ) {

    //Set the database question
    $this->db->question( 'SELECT information FROM periods WHERE id = :id' );
    
    //Bind the id parameter
    $this->db->bind( ':id', $id );

    //Try execution of question
    //If execution is profitable
    if( $this->db->execute() ){

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

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

    }

    //If execution is just not profitable
    else {

        // Return an empty string
        return '';

    }

}

The explanation this error happens is that PHP variations 7.1 and above now strictly require the session _read to return a string worth, which excludes even returning a null worth. The code under is the place a null worth has the opportunity of being returned (when $row[ ‘data’ ] is definitely null):

//Try execution of question
//If execution is profitable
if( $this->db->execute() ){

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

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

}

You’ll see the else assertion proper under is returning ” upon failure of execution. Nonetheless, there is no such thing as a examine as as to if or not $row[ ‘data’ ] is returning now. All you need to merely do is return ” when $row[ ‘data’ ] is null, ensuing within the following edit to the code above:

//Try execution of question
//If execution is profitable
if( $this->db->execute() ){

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

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

        return '';

    }

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

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

    }
}

Your last snippet of code ought to appear like this:

public perform _read( $id ) {

    //Set the database question
    $this->db->question( 'SELECT information FROM periods WHERE id = :id' );

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

    //Try execution of question
    //If execution is profitable
    if( $this->db->execute() ){

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

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

            return '';

        }

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

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

        }

    }
    //If execution is just not profitable
    else {

        // Return an empty string
        return '';

    }

}

 

You may also like

Platform Slot Gacor CMS Checker