Welcome Guest, Not a member yet? Register   Sign In
How to display all session variables
#1

[eluser]fserrano[/eluser]
How can I quickly display all my session variables in the session array? I can't see the answer to this question in the User Guide Session Class page.

This wouldn't work:
Code:
echo "<pre>";
echo print_r($this->session());
echo "</pre>";

This wouldn't work either:
Code:
echo "<pre>";
echo print_r($_SESSION);
echo "</pre>";
User Guide
#2

[eluser]edjon2000[/eluser]
Hi fserrano,

all you need to do is modify your code slightly, instead of
Code:
echo "<pre>";
echo print_r($this->session()); // or echo print_r($_SESSION);
echo "</pre>";
use
Code:
echo "<pre>";
print_r($this->session); // or print_r($_SESSION);
echo "</pre>";

print_r is a command so doesn't need to be echoed.

another thing you can try if you need to identify data types as well, is
Code:
echo "<pre>";
var_dump($this->session); // or var_dump($_SESSION);
echo "</pre>";


here is a link with further information http://php.net/manual/en/function.print-r.php


Hope this helps you
Jon
#3

[eluser]Cristian Gilè[/eluser]
$this->session() is not valid!

If you want to print the object content the right way is:
Code:
print_r($this->session);

without parentheses.

To extract a single element you could use the element function in the array helper:
Code:
$this->load->helper('array');
echo element('session_id',get_object_vars($this->session));

get_object_vars is needed to get the properties of the session object.

Cristian Gilè
#4

[eluser]edjon2000[/eluser]
Lol Cristian,

I can't believe I missed that in my response, thanks for the correction, I have updated my post.

Jon
#5

[eluser]fserrano[/eluser]
Ah, thanks. This worked:

Code:
echo "<pre>";
echo print_r($this->session);
echo "</pre>";
#6

[eluser]edjon2000[/eluser]
[quote author="fserrano" date="1295044272"]Ah, thanks. This worked:

Code:
echo "<pre>";
echo print_r($this->session);
echo "</pre>";
[/quote]

Hmm even with the echo on the second line ?

Jon
#7

[eluser]Cristian Gilè[/eluser]
The return value of print_r is TRUE, so you can echo TRUE but it is useless.


Cristian Gilè
#8

[eluser]edjon2000[/eluser]
Yeah thats what I thought too Cristian

I must admit I have used print_r and var_dump since I started programming in PHP, and it is helping me out with my current web project which I decided to build using CodeIgniter quite a learning curve for me at the moment, so you will no doubt see some fairly newbie qustions around this forum from me. actually, that raises a question, I have a few useful functions that I use routinely for debugging web projects to such an extent that I put together a funcs.php file which I just included in whatever I was working on whilst developing a site, In CI however, you can't include files in the same way, could useful code snippets like the print_r and the var_dump statements be added in such a way as to make them globally available within a CI project(normally in a traditional XHTML/PHP/MySQL site I would include the funcs.php file and just call them individually as functions) so I would have a function like
Code:
function show_array($array)
{
  echo '<pre';
  print_r($array};
  echo '</pre>';
}
and just call it with
Code:
show_array($_POST};

show_array($_SESSION);

show_array($any-other-array);
and so on, by doing it this way it made looking inside arrays very easy and could well help ppl like fserrano and me and others avoid typos

What are your thoughts on this

Jon
#9

[eluser]Cristian Gilè[/eluser]
To organize your debug functions you can put them in a helper, to make them available globally, and also turn on/off the output depending on a config variable.

Take a look at this helpful helper by Phil Sturgeon


Cristian Gilè
#10

[eluser]edjon2000[/eluser]
Ah ok thanks Cristian,

I hope I have not hi-jacked this thread, it is just that my my question was loosely related to fserrano's original post.

Jon




Theme © iAndrew 2016 - Forum software by © MyBB