Welcome Guest, Not a member yet? Register   Sign In
Storing HTML in session?
#1

[eluser]drshields[/eluser]
Does CI have a problem with storing HTML in a session?

I am storing some html (form data) in a session variable. On the controller that I store that information, it is accessible and works as it should and on other controllers, it has a null value. Here is some code to show an example:

Controller A:

Code:
$form = '<form action="#" method="post"><input type="hidden" name="a" value="a"></form>';

$user_data = array('html_form' => $form, 'form_title' => 'Form Title');
$this->session->set_userdata($user_data);

// now if I stay in this form, these both work:

echo $this->session->userdata('form_title'); // prints 'Form Title'
echo $this->session->userdata('html_form'); // prints <form...

Controller B:

Code:
echo $this->session->userdata('form_title'); // prints 'Form Title' correctly
echo $this->session->userdata('html_form'); // NO (NULL?) VALUE HERE

Why isn't can't this form data be shared between controllers like this?
#2

[eluser]garymardell[/eluser]
Just out of curiosity why would you need to?
#3

[eluser]drshields[/eluser]
I am working with an another websites API. I'm getting this data returned in an XML REST query, and I need to have access to this data between controllers for a short time. I could write it to the DB but I don't think it would be very efficient. Also, I just tested and storing it in a cookie actually works, but I'd rather keep it in a session if possible.
#4

[eluser]TheFuzzy0ne[/eluser]
The data saved in the session isn't sanitised or changed in any way, so it is a strange issue indeed. As you can access the title from within the other controller, it's almost starting to sound like a PHP bug...

What version of PHP are you running?

EDIT: My suggestion be to try storing a simple string (letters only), and see if anything changes. Another alternative might be to perhaps change the html_form key to something slightly different, and see if you are enlightened any further as to where the problem may lie.
#5

[eluser]drshields[/eluser]
There some other weird stuff going in. In Firefox, I get this error very frequently when loading the page:

Quote:A PHP Error was encountered
Severity: Warning
Message: serialize() [function.serialize]: Node no longer exists
Filename: libraries/Session.php
Line Number: 281

This is happening randomly it seems; sometimes I can refresh the page and it goes away, othertimes I need to clear private data in firefox and restart the browser, then the error goes away, but it mysteriously comes back. It doesn't happen in any other browser. Could it be related?

Regarding testing different things, I've tested in it many different ways. I've even taken the html out of the variable, and then it works fine, example modifying the code from above:

Code:
$form = 'This is usually where form data is...';

$user_data = array('html_form' => $form, 'form_title' => 'Form Title');
$this->session->set_userdata($user_data);

echo $this->session->userdata('html_form'); // Works as expected across all controllers

Edit: PHP 5.1.6
#6

[eluser]TheFuzzy0ne[/eluser]
I think the best thing you could probably do (if you haven't done so already), is install:

The Web Developer extension for Firefox and
the Firebug extension for Firefox

Using Firebug you can see exactly how Firefox is rendering the data, and if anything is being changed or removed. The Web Developer extension allows you to validate local HTML, and will tell you if anything isn't right.

Personally, I don't know how I ever lived without them both. I've learned that most problems I seem to get are due to pages not validating. It also makes it easier to track a problem back to the server code.
#7

[eluser]drshields[/eluser]
Okay, I figured it out. For whatever reason (and perhaps somebody with more expertise can answer why) putting double quotes around the variable worked.

Code:
$form = '<form action="#" method="post"><input type="hidden" name="a" value="a"></form>';

$user_data = array('html_form' => "$form", 'form_title' => 'Form Title');
$this->session->set_userdata($user_data);

I don't understand why, but that's what I needed to do. Thanks everyone for looking at it.
#8

[eluser]TheFuzzy0ne[/eluser]
Well done.

I would never have thought of using double quotes. Perhaps it's parse by PHP differently, or it genuinely is a bug. At least I now know what to do should I ever experience similar problems.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB