Page 1 of 1

Serializing and Unserializing

Posted: Tue Jun 19, 2007 6:23 am
by shiznatix
Ok so I am making a multi lingual website and I am saving all the dynamic text as a serialized array in the database then unserializing it when I need to display it. Everything is going fine with this method, except this 1 thing is not working. I have this long string with HTML and text in it and I am trying to save it in a serialized array. No problems there but when I unserialize it, I get an error. I have tried everything to no avail.

Here is a simplified test case that is failing. I start with this code:

Code: Select all

$str = 'Follow these steps to sign up to NoIqPoker, and to get included in their VIP Cash Bonus program. iPoker rakeback is not available, but this VIP Cash Bonus program is a very good alternative.
<br />
<br />
<img src="images/rb_bullet_orange.gif" border="0" /><b>Example:</b> <font color="#FF0000">$1</font> in tournament fees equals <font color="#FF0000">$0.39</font> VIP Cash Bonus, when you are at VIP Level Black and get 40 FPP. See the <a href="noiqpoker/offers/">NoiQPoker Offers</a> page for VIP tables and a bonus calculator. 
<h2>NoIqPoker VIP Cash Bonus Program Sign Up</h2> 
<ol>
	<li>
		<a href="poker-forum/register.php">Register an account</a> to Rakeback.com website and log in.
	</li>
	<li>
		Download and install NoIqPoker software: 
		<br />
		<img src="images/rb_bullet_orange.gif" border="0" alt="" /><a href="http://www.noiqpoker.com/download.html" target="_blank">www.noiqpoker.com/download.html</a>
	</li>
	<li>
		Open NoIqPoker and click <b>Create Account</b>.
		<br />
		<img src="images/rb_bullet_orange.gif" border="0" alt="" />Enter our code <b>RAKECOM</b> in the <i>Signup code</i> field.
		<br />
		<br />
		<img src="images/noiqpoker-code.jpg" border="0" alt="NoIqPoker Signup code" width="378" height="125" />
	</li>
</ol>
<h2>Submit NoIqPoker Sign Up Form</h2>
After opening your NoIqPoker account, please submit your <b>NoIqPoker Username</b> to us with the form below. You need to be logged in to our website to do this. Our Support will then verify your NoIqPoker sign up and send you a confirmation email.';

$arr = array('en' => $str, 'br' => '', 'cn' => '', 'de' => '', 'es' => '', 'it' => '', 'se' => '');
$thing = serialize($arr);
echo $thing;
Then that outputs this:

Code: Select all

a:7:{s:2:"en";s:1742:"Follow these steps to sign up to NoIqPoker, and to get included in their VIP Cash Bonus program. iPoker rakeback is not available, but this VIP Cash Bonus program is a very good alternative.
<br />
<br />
<img src="images/rb_bullet_orange.gif" border="0" /><b>Example:</b> <font color="#FF0000">$1</font> in tournament fees equals <font color="#FF0000">$0.39</font> VIP Cash Bonus, when you are at VIP Level Black and get 40 FPP. See the <a href="noiqpoker/offers/">NoiQPoker Offers</a> page for VIP tables and a bonus calculator.
<h2>NoIqPoker VIP Cash Bonus Program Sign Up</h2>
<ol>

        <li>
                <a href="poker-forum/register.php">Register an account</a> to Rakeback.com website and log in.
        </li>
        <li>
                Download and install NoIqPoker software:
                <br />
                <img src="images/rb_bullet_orange.gif" border="0" alt="" /><a href="http://www.noiqpoker.com/download.html" target="_blank">www.noiqpoker.com/download.html</a>
        </li>

        <li>
                Open NoIqPoker and click <b>Create Account</b>.
                <br />
                <img src="images/rb_bullet_orange.gif" border="0" alt="" />Enter our code <b>RAKECOM</b> in the <i>Signup code</i> field.
                <br />
                <br />

                <img src="images/noiqpoker-code.jpg" border="0" alt="NoIqPoker Signup code" width="378" height="125" />
        </li>
</ol>
<h2>Submit NoIqPoker Sign Up Form</h2>
After opening your NoIqPoker account, please submit your <b>NoIqPoker Username</b> to us with the form below. You need to be logged in to our website to do this. Our Support will then verify your NoIqPoker sign up and send you a confirmation email.";s:2:"br";s:0:"";s:2:"cn";s:0:"";s:2:"de";s:0:"";s:2:"es";s:0:"";s:2:"it";s:0:"";s:2:"se";s:0:"";}
No problem. But then I copy that string and try to unserialize it and I get an error. I do exactally this code:

Code: Select all

$str2 = 'a:7:{s:2:"en";s:1742:"Follow these steps to sign up to NoIqPoker, and to get included in their VIP Cash Bonus program. iPoker rakeback is not available, but this VIP Cash Bonus program is a very good alternative.
<br />
<br />
<img src="images/rb_bullet_orange.gif" border="0" /><b>Example:</b> <font color="#FF0000">$1</font> in tournament fees equals <font color="#FF0000">$0.39</font> VIP Cash Bonus, when you are at VIP Level Black and get 40 FPP. See the <a href="noiqpoker/offers/">NoiQPoker Offers</a> page for VIP tables and a bonus calculator.
<h2>NoIqPoker VIP Cash Bonus Program Sign Up</h2>
<ol>

        <li>
                <a href="poker-forum/register.php">Register an account</a> to Rakeback.com website and log in.
        </li>
        <li>
                Download and install NoIqPoker software:
                <br />
                <img src="images/rb_bullet_orange.gif" border="0" alt="" /><a href="http://www.noiqpoker.com/download.html" target="_blank">www.noiqpoker.com/download.html</a>
        </li>

        <li>
                Open NoIqPoker and click <b>Create Account</b>.
                <br />
                <img src="images/rb_bullet_orange.gif" border="0" alt="" />Enter our code <b>RAKECOM</b> in the <i>Signup code</i> field.
                <br />
                <br />

                <img src="images/noiqpoker-code.jpg" border="0" alt="NoIqPoker Signup code" width="378" height="125" />
        </li>
</ol>
<h2>Submit NoIqPoker Sign Up Form</h2>
After opening your NoIqPoker account, please submit your <b>NoIqPoker Username</b> to us with the form below. You need to be logged in to our website to do this. Our Support will then verify your NoIqPoker sign up and send you a confirmation email.";s:2:"br";s:0:"";s:2:"cn";s:0:"";s:2:"de";s:0:"";s:2:"es";s:0:"";s:2:"it";s:0:"";s:2:"se";s:0:"";}';

$thing = unserialize($str2);

print_r($thing);
die();
and I get this error:
Notice: unserialize() [function.unserialize]: Error at offset 1585 of 1690 bytes in /var/www/html/test.php on line 58

And as I understand this error it means the byte count is wrong somewhere but I don't understand why in the world is it wrong? I am changing nothing from the original serialized array, this does not make sense and of all the string I have been serializing with lots of HTML and other sorts of crazy stuff this is the only one that is giving me any trouble anywhere. Am I missing something stupid?

Posted: Tue Jun 19, 2007 6:26 am
by superdezign
Try serializing with htmlspecialchars() and unserializing with html_entity_decode().

Edit: Actually, try addslashes() and stripslashes(). There may be problems with the double quotes in your HTML.

Posted: Tue Jun 19, 2007 7:11 am
by Begby
You might want to rethink your approach to how you are doing this. For one thing lets say you have 10 languages and 100 different snippets like this for each language, that is 1000 snippets.

Now lets say you want to change the font color of the money from #FF0000 to #00FF00. Now you have a very serious problem.

At the very least use CSS which will mitigate the problem somewhat, but ideally you should rethink how you have your system setup so that all that HTML isn't stored in the DB.

Posted: Tue Jun 19, 2007 7:16 am
by shiznatix
superdezign wrote:Try serializing with htmlspecialchars() and unserializing with html_entity_decode().

Edit: Actually, try addslashes() and stripslashes(). There may be problems with the double quotes in your HTML.
superdezign: It isn't the quotes or anything, I have many other strings with double quotes and HTML and everything, no problems. And if I serialize that first array and instead of copying it to another file and trying to unserialize it, if I just right away unserialize it then there are no problems. It is a much stranger problem.
Begby wrote:You might want to rethink your approach to how you are doing this. For one thing lets say you have 10 languages and 100 different snippets like this for each language, that is 1000 snippets.

Now lets say you want to change the font color of the money from #FF0000 to #00FF00. Now you have a very serious problem.

At the very least use CSS which will mitigate the problem somewhat, but ideally you should rethink how you have your system setup so that all that HTML isn't stored in the DB.
I am quite aware of this but right now the site is only in 1 language and all of this HTML is stored in the database and they want a multi lingual site up like right away. I will have plenty of time later on to fix all of those issues, I am just getting the framework and first design out. Im taking out the biggest problems first then that will calm down everyone so I can work on the small little things.

Re: Serializing and Unserializing

Posted: Tue Jun 19, 2007 7:16 am
by volka
When I run the code the serialized string starts with
a:7:{s:2:"en";s:1742: , not
a:7:{s:2:"en";s:1563:

Re: Serializing and Unserializing

Posted: Tue Jun 19, 2007 7:21 am
by shiznatix
volka wrote:When I run the code the serialized string starts with
a:7:{s:2:"en";s:1742: , not
a:7:{s:2:"en";s:1563:
Aye yes same here I don't know how in the world I originally got the 1563 but I re-ran the scripts and got the 1742 like I should. Still though, the problem still persists either after running everything again.

Posted: Tue Jun 19, 2007 7:56 am
by volka
Does the problem only occur when you try to unserialize the string that you retrieved from the database?

Posted: Tue Jun 19, 2007 9:04 am
by shiznatix
volka wrote:Does the problem only occur when you try to unserialize the string that you retrieved from the database?
It happens if I serialize that array then after the page is done executing I try to copy the serialized string to anywhere, a flat text file, in a variable, or in the database and whenever I pull it out and try to unserialize it I get the error. I have tried reserializing it a million times to no avail and I don't see what the problem is, this is the only string out of like 200 much more complex strings that is giving me any troubles and I am using the same exact system for all arrays being serialized.

Posted: Tue Jun 19, 2007 10:52 am
by volka
shiznatix wrote:It happens if I serialize that array then after the page is done executing I try to copy the serialized string to anywhere, a flat text file, in a variable, or in the database and whenever I pull it out and try to unserialize it I get the error.
Don't know how you copy the text but it seems youÄre doing something wrong ;)
please try

Code: Select all

<?php
$str = 'Follow these steps to sign up to NoIqPoker, and to get included in their VIP Cash Bonus program. iPoker rakeback is not available, but this VIP Cash Bonus program is a very good alternative.
<br />
<br />
<img src="images/rb_bullet_orange.gif" border="0" /><b>Example:</b> <font color="#FF0000">$1</font> in tournament fees equals <font color="#FF0000">$0.39</font> VIP Cash Bonus, when you are at VIP Level Black and get 40 FPP. See the <a href="noiqpoker/offers/">NoiQPoker Offers</a> page for VIP tables and a bonus calculator.
<h2>NoIqPoker VIP Cash Bonus Program Sign Up</h2>
<ol>
        <li>
                <a href="poker-forum/register.php">Register an account</a> to Rakeback.com website and log in.
        </li>
        <li>
                Download and install NoIqPoker software:
                <br />
                <img src="images/rb_bullet_orange.gif" border="0" alt="" /><a href="http://www.noiqpoker.com/download.html" target="_blank">www.noiqpoker.com/download.html</a>
        </li>
        <li>
                Open NoIqPoker and click <b>Create Account</b>.
                <br />
                <img src="images/rb_bullet_orange.gif" border="0" alt="" />Enter our code <b>RAKECOM</b> in the <i>Signup code</i> field.
                <br />
                <br />
                <img src="images/noiqpoker-code.jpg" border="0" alt="NoIqPoker Signup code" width="378" height="125" />
        </li>
</ol>
<h2>Submit NoIqPoker Sign Up Form</h2>
After opening your NoIqPoker account, please submit your <b>NoIqPoker Username</b> to us with the form below. You need to be logged in to our website to do this. Our Support will then verify your NoIqPoker sign up and send you a confirmation email.';

$arr = array('en' => $str, 'br' => '', 'cn' => '', 'de' => '', 'es' => '', 'it' => '', 'se' => '');
$thing = serialize($arr);


// test #1
$test = unserialize($thing);
echo '#1 ' , count($test), "<br />\n";

// test #2
file_put_contents('serialized.txt', $thing);
$test = file_get_contents('serialized.txt');
$test = unserialize($test);
echo '#2 ' , count($test), "<br />\n";

Posted: Wed Jun 20, 2007 9:25 am
by shiznatix
I have no idea why it was not working for me but I ran your script, coppied the serialized string from the text file, and manually put that in the DB and it worked perfectly. Makes 0 sense to me but, success!

Posted: Wed Jun 20, 2007 10:11 am
by Ambush Commander
I think the culprit here is conflicting newlines. When you copied it over, \n got changed to \r\n or vice versa. I've had this problem before with Subversion dumps, which use a similar data-format that specifies byte length. I transferred it over FTP in text mode, the client converted the newlines and consequently destroyed the Subversion dump.