473,503 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unserialize(), arrays, and newlines = error

Greetings:

So, I've got a real problem in that PHP doesn't seem to be able to
unserialize() an array that has an element containing a newline. Of
course, I've used htmlentities() to protect the serialized data.

Here's a simple example that demonstrates the problem:
-----
<?php

$blah = array("I like\nnewlines");

echo '<html>';
echo '<body>';

if (isset($_POST['blah'])) {
if (unserialize(html_entity_decode($_POST['blah'])) === FALSE)
echo '<p>Unserialize failed.</p>';
else
echo '<p>Unserialize worked.</p>';
}

echo '<form method="POST" enctype="multipart/form-data"
action="test.php">';

echo '<input type="hidden"';
echo ' name="blah"';
echo ' value="';
echo htmlentities(serialize($blah));
echo '"';
echo '/>';

echo '<input type="submit" name="doink" value="Submit" />';

echo '</form>';

echo '</body>';
echo '</html>';

?>
Jul 17 '05 #1
3 8109
Mark wrote:
So, I've got a real problem in that PHP doesn't seem to be able to
unserialize() an array that has an element containing a newline. Of
course, I've used htmlentities() to protect the serialized data.

Here's a simple example that demonstrates the problem:
two small changes, just to remove that newline

[...]if (isset($_POST['blah'])) { if (unserialize(stripcslashes(html_entity_decode($_PO ST['blah'])))
=== FALSE) echo '<p>Unserialize failed.</p>'; [...]echo ' value="'; echo htmlentities(addcslashes(serialize($blah), "\n"));echo '"';

[...]
Depending on the contents of your "blah" field you may want to add
more characters to be added cslashes.
HTH

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #2
Greetings:

Pedro <he****@hotpop.com> wrote in message news:<3b********************************@4ax.com>. ..
two small changes, just to remove that newline
[...]
Depending on the contents of your "blah" field you may want to add
more characters to be added cslashes.


Thanks for the suggestion Pedro. PHP only seems to have problems when
the serialize()d data is coming from a form. If I just serialize()
and htmlentities() the array, and then do the reverse without going
through the form, everything is just fine.

So obviously this isn't a bug with PHP, rather it's something in how
the form data is handled, and not necessarily a bug. In any case, it
certainly caught me off guard.

Thanks again.

Mark A. Fox
Jul 17 '05 #3
Greetings:

Pedro led me to the final and general (I think) solution to this
problem: Use addcslashes() to slashify all non-ASCII characters.
This is important in my case (but not the example below), because I'm
dealing with what could be binary data.

Here's the final updated example:

-----
<?php

$blah = array("I like\nnewlines");

echo '<html>';
echo '<body>';

if (isset($_POST['blah'])) {
if (unserialize(html_entity_decode(stripcslashes($_PO ST['blah'])))
=== FALSE)
echo '<p>Unserialize failed.</p>';
else
echo '<p>Unserialize worked.</p>';
}

echo '<form method="POST" enctype="multipart/form-data"
action="test.php">';

echo '<input type="hidden"';
echo ' name="blah"';
echo ' value="';
echo htmlentities(addcslashes(serialize($blah),
"\0..\37!@\@\177..\377"));
echo '"';
echo '/>';

echo '<input type="submit" name="doink" value="Submit" />';

echo '</form>';

echo '</body>';
echo '</html>';

?>
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2652
by: James Sleeman | last post by:
Hi all, i just spent an hour rifling through code to find the cause of a problem only to find it's an oddity with serialization and recursive objects, so figured I'd post for the next person who...
2
2782
by: Andrew | last post by:
Some have suggested that using serialize() and unserialize is faster than reading/writing an array to disk as a simple text file using $array = file('numbers.txt'); Can anyone justify this? ...
7
5132
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For...
5
3756
by: Mike | last post by:
I finally got serialize to work but now I cannot get unserialize to work. Here is my code. $settings = serialize($settings_array); echo "<INPUT TYPE='Hidden' NAME='settings'...
1
1783
by: dbee | last post by:
Hi, So I'm having a problem with disappearing newlines. I import the newlines from a file into my shell script fine. But then I process the text and the url_encode comes out the other end with...
4
2162
by: bei | last post by:
Hi, I am trying to write several arrays into one file, with one arrays in one column. Each array (column) is seperated by space. ie. a= b= c= 1 5 9 2 6 10 3 7 11 4 8 12
7
3534
by: powellgg | last post by:
I've just taken over a PHP website and am converting it to ASP.NET (don't shoot!). I'm not a PHP guy so I'm doing a lot of searching for things that I aren't obvious, and I'm hoping I'll be able...
0
1173
The1corrupted
by: The1corrupted | last post by:
I have tried over and over but I acnnot get serialized arrays in php to unserialize and register.
0
1141
by: kigoobe | last post by:
Hi friends I am using serialize to store a multidimensional array to the db, which looks like the following on printing directly from the database ...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.