OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



PHP Notice: unserialize(): Error at offset

Are you responsible for making OCS work -- installing, upgrading, migrating or troubleshooting? Do you think you've found a bug? Post in this forum.

Moderators: jmacgreg, michael, John

Forum rules
What to do if you have a technical problem with OCS:

1. Search the forum. You can do this from the Advanced Search Page or from our Google Custom Search, which will search the entire PKP site. If you are encountering an error, we especially recommend searching the forum for said error.

2. Check the FAQ to see if your question or error has already been resolved. Please note that this FAQ is OJS-centric, but most issues are applicable to both platforms.

3. Post a question, but please, only after trying the above two solutions. If it's a workflow or usability question you should probably post to the OCS Conference Support and Discussion subforum; if you have a development question, try the OCS Development subforum.

PHP Notice: unserialize(): Error at offset

Postby GeneTitus » Fri Apr 12, 2013 12:28 pm

While working on migrating to the newest version of OCS, we ran into several...
PHP Notice: unserialize(): Error at offset...
errors. Looking through older PKP forums, this problem was described as a problem with php changing the way it performed serialize and unserialize over php releases. I don't believe that is the problem.

The problem, for us, was seen when special characters were input into OCS forms and stored into mysql. On our older system, php, mysql, and the php mysql library all were using UTF-8 encoding. So special characters were stored as (potential) multi byte characters. This can be demonstrated by the following php code:

echo strlen('héllö'); // will return 7 in UTF8 encoding

(side note: php.net says "serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field.")

Now we build a new system for our updated OCS, mysql dump the old database-> reload to new mysql database, keeping the UTF-8 encoding.

When we run the update script,we get several: PHP Notice: unserialize(): Error at offset... for any mysql value that contains special characters because....
The newer version of php mysql defaulted to latin1 encoding rather than utf-8, so the special characters were now one byte long, rather than 2 bytes, and the unserialize command complained that the string count was off.

The problem is not with php's implementation of serialize and unserialize. It's when you change encoding schemes in mid stream.

The fix:

If there is a way to force php 5.3.* mysql to default to UTF-8 encoding by some configuration file setting, I could not find it. So in the file:

lib/pkp/lib/pqp/classes/MySqlDatabase.php

I added the line:
mysql_set_charset('utf8',$this->conn);

after the database connection was made.

The function now looks like:
function connect($new = false) {
$this->conn = mysql_connect($this->host, $this->user, $this->password, $new);
if(!$this->conn) {
throw new Exception('We\'re working on a few connection issues.');
}
mysql_set_charset('utf8',$this->conn);
}

That forced the mysql connect to use utf-8 encoding and the solved the unserialized errors.

(side note from php.net: mysql_set_charset - This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.)

Regards,
Gene
Texas Digital Library
GeneTitus
 
Posts: 3
Joined: Fri Apr 12, 2013 11:58 am

Re: PHP Notice: unserialize(): Error at offset

Postby asmecher » Fri Apr 12, 2013 12:34 pm

Hi Gene,

That ought to be covered by the database_charset and connection_charset settings in your config.inc.php file, no? What do you have for those settings?

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 8851
Joined: Wed Aug 10, 2005 12:56 pm

Re: PHP Notice: unserialize(): Error at offset

Postby GeneTitus » Fri Apr 12, 2013 12:40 pm

Good catch!
connection_charset = Off
database_charset = Off

I'm guessing I should set those to utf-8

Gene
GeneTitus
 
Posts: 3
Joined: Fri Apr 12, 2013 11:58 am

Re: PHP Notice: unserialize(): Error at offset

Postby asmecher » Fri Apr 12, 2013 12:45 pm

Hi Gene,

Not sure if it matters, but just in case -- mine are "utf8", not "utf-8".

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 8851
Joined: Wed Aug 10, 2005 12:56 pm

Re: PHP Notice: unserialize(): Error at offset

Postby GeneTitus » Fri Apr 12, 2013 1:02 pm

I set them to utf-8.

That worked!!!

Thanks,
Gene
GeneTitus
 
Posts: 3
Joined: Fri Apr 12, 2013 11:58 am


Return to OCS Technical Support

Who is online

Users browsing this forum: Bing [Bot] and 2 guests