Forum Moderators: coopster

Message Too Old, No Replies

Magic quotes and session data

line breaks are giving me issues

         

sned

8:09 pm on Oct 27, 2008 (gmt 0)

10+ Year Member



Here's something weird I found:

So say I put some data into a session field from a textarea:

This is session data.

There are line breaks and 'quotes'

Now, when I want to use that session data on another page, the resulting data is slightly confusing:

If magic quotes is on, the results are as expected:


echo '<pre>';
print_r($_SESSION['myvar']);
echo '</pre>';

prints out something like:

This is session data.

There are line breaks and \'quotes\'

Notice that the line breaks are preserved.

However, if magic quotes are off, the line breaks are not preserved:

This is session data.\r\n\r\nThere are line breaks and \'quotes\'

My issue is that nl2br does not work on the \r\n values.

Has anyone else run into this issue, or have a fix?

(str_replace('\r\n', '<br />', $value) works on this, but seems more like a kludge than anything ...)

Thanks!
-Sned

sned

9:08 pm on Oct 27, 2008 (gmt 0)

10+ Year Member



Edit time has run out, but now I realize that the data is being passed through an escaping function before it gets sent to session data, which is the reason for the weird behavior.

Still not sure how to make nl2br work on escaped data.

-Sned

cameraman

12:56 am on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your results seem backward to me, but I don't see your solution as being a kludge; I would have either used that or a regular expression.

dreamcatcher

8:10 am on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its strange that the slashes appear with Magic Quotes off. Ideally you should code with them OFF because this directive is being removed in PHP6. And about time too.

From PHP.net

This feature has been DEPRECATED and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

There are no comments on the PHP site about magic quotes causing line break issues with nl2br. Are you running the code through any other functions?

dc