473,556 Members | 2,460 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(ht ml_entity_decod e($_POST['blah'])) === FALSE)
echo '<p>Unserializ e failed.</p>';
else
echo '<p>Unserializ e worked.</p>';
}

echo '<form method="POST" enctype="multip art/form-data"
action="test.ph p">';

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

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

echo '</form>';

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

?>
Jul 17 '05 #1
3 8115
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(st ripcslashes(htm l_entity_decode ($_POST['blah'])))
=== FALSE) echo '<p>Unserializ e failed.</p>'; [...]echo ' value="'; echo htmlentities(ad dcslashes(seria lize($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(ht ml_entity_decod e(stripcslashes ($_POST['blah'])))
=== FALSE)
echo '<p>Unserializ e failed.</p>';
else
echo '<p>Unserializ e worked.</p>';
}

echo '<form method="POST" enctype="multip art/form-data"
action="test.ph p">';

echo '<input type="hidden"';
echo ' name="blah"';
echo ' value="';
echo htmlentities(ad dcslashes(seria lize($blah),
"\0..\37!@\@\17 7..\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
2660
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 gets the same problem (y'all might find it interesting too)... ********************************* Symptoms ********************************* When...
2
2785
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? It seems to me that there is some overhead associated with calling unserialize().
7
5134
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 example, given: $data = serialize(array('4.50','0.00','0.00'));
5
3758
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' VALUE='".$settings."'>"; $settings = $_REQUEST; echo "<P>us=".$settings; $settings_array = Array();
1
1796
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 linefeeds intact but no newlines. Also any single quotes in my file give me a T_STRING unexpected error in my output ... even though I'm using...
4
2169
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
3548
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 to get help from here for things I can't find on the Internet. Here's the first... Here's the PHP code: setcookie("site_search_keyword","",...
0
1182
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
1151
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 s:175:"a:5:{i:1;a:2:{i:0;s:4:"11~2";i:1;s:4:"61~1";}i:2;a:1:{i:0;s:4:"52~2";}i:4;a:2:{i:0;s:4:"36~1";i:1;s:4:"38~2";}i:5;a:1:{i:0;s:4:"41~1";}i:3;a:1:{i:0;s:4:"44~2";}}"; However, when I...
0
7622
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7547
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8060
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3596
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
865
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.